Creating json object without keys from array in android - android

Is there any way to create json object with only values and no keys, from array list in android?
I have array list like this
[12,name,description]
But i need to convert this to JSON Object like this
{"12","name","description"}
How to do this? Can anyone help?

No, you cannot create JSONObject without its key.
A modifiable set of name/value mappings. Names are unique, non-null
strings.
Source
Definition itself says names are unique, non-nullso you need to use key-value pair only, however it it possible to create JSONArray like this, but in that case also that JSONArray have some key.
"someKey":["a","b","c"]
So in this case also JSONArray needs to define under some key.

Related

public JSONObject (String json) in api19 scrambles order?

I was parsing a string to a JSONObject and noticed some weird behaviour. The orders of items get scrambled? Is this a bug or did i do something wrong?
Your 'items' are key/value pairs in a JSON object.
The order of key/value pairs within a JSON object is not guaranteed:
An object is an unordered set of name/value pairs.
If you need to preserve the order of those items you can put them in an array on the serializing side (probably a server):
An ordered list of values. In most languages, this is realized as an
array, vector, list, or sequence.
Official docs.

How to get Json String without set data in getString("name")?

Im doing a dynamic form to Android and I need just read a Json file and build a form and it should work in anyway. How can I do it? In "name" getString("name") what I can put there to get a name without write "name" inside de method?
Is the question that you want to find the keys of a JSONArray? Iterate through the array, grab the key of each object. Then, grab the value.
See second answer: Retrieving Keys from JSON Array key-value pair dynamically - Javascript
I think the following should help:
http://developer.android.com/reference/org/json/JSONObject.html#keys()
You can use this Iterator to iterate through all the keys and then use the key to extract the desired values.
Additionally, if dealing with a JSONArray you could use the following too:
http://developer.android.com/reference/org/json/JSONObject.html#names()

Get JSON Object Keys in a loop with default order android

I want to parse JSONObject and get its keys in the order they do represent to me when i receive. I don't want use JSONObject.keys() as this function giving undefined order of json keys and fully unordered,and this is a huge problem right now because I'm stuck in this position and i have to get json object keys.So is there any way to achieve it? Doing researches i never came across to code which can parse json object keys except JSONObject.keys() which giving reverse order.
There is no way to achieve what you want with a JSONObject because JSONObjects are not ordered by definition and you shouldn't rely on the insertion order. If you need something ordered you should look into JSONArray
I have noticed that although JSONArray gives the proper order, the order won't be kept in HashMap, if you're using it for saving the values.
Using LinkedHashMap solves the order issue.

Android: Convert web service json output into java hashmap

I have the following output, where wsResponse.get("result").toString()
= {"status":1,"result":{"2011":102003,"2010":100003,"2009":98723,"2008":129023}}
I'll like to create a hashmap where the years are the key.
How will i be able to populate the hashmap?
Is it necessary to have a hashmap? Android has the org.json library, so you can do something like:
JSONObject json = new JSONObject(wsResponse.get("result").toString());
If you check out the reference, you can use JSONObject just like a hashmap. You can call get(String key) to get the value for a year.
If you must have a hashmap, you can iterate through the JSONObject and put the keys and values in the hashmap.
Take a look at JSONObject.

Comparing JSONArrays

I am currently using a custom service in my android application to retrieve a JSONArray from a remote database. Once I have retrieved the JSONArray, I would like to compare its contents to another JSONArray which I call 'mostRecent.' If the newly retrieved JSONArray differs from 'mostRecent' then I would like to update 'mostRecent' with the newly retrieved JSONArray. If I am not mistaken, however, the JSONArray equals method compares the instance of the JSONArray object and not the contents, correct? Is there an easy method to compare the contents of two JSONArrays? Thanks for the help!
Use that:
array1.toString().equals(array2.toString());
Maybe you will be able to use even the strings you receive without constructing the JSONArrays, but then you will need to make sure the two are formatted the same way.

Categories

Resources