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.
Related
I have a Json string:
String json = "{\"I\":0,\"lst\":[{\"i1\":100500,\"s1\":\"abrakadabra\",
\"aList\":[{\"text\":\"secret will of my Dad\"}]}]}";
JSONObject o = new JSONObject(json);
My question: how, using Json Obj methods, to browse through each node element recursively?
Vitali, I don't have enough reputation points to reply to your comment, so posting it as an answer. In that post I linked, I meant the code snippet with loopThroughJson() method. I haven't tried it myself but that looks right. For completeness, this is the link again -
Recursively parsing JSON via JSONObject to fetch value against specific keys
Loop through the object, get child as reference of Object class using the get() method, if that object is instance of JSONObject or JSONArray, go deeper.
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.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sort JavaScript object by key
I am creating JSONArray from JSONObject in android. The JSONArray that i wanted to be is:
[{"last_name":"cruz",
"first_name":"juan",
"middle_name":"sam"}]
but it appears
[{"first_name":"cruz",
"last_name":"juan",
"middle_name":"sam"}]
how can I arrange the array in order that I wanted?
Thanks...
1. prepare a LinkedHashMap object with elements
2. convert it to JSONObject
Example:
Map obj = new LinkedHashMap();
obj.put("a", "String1");
obj.put("b", new Integer(1));
obj.put("c", new Boolean(true));
obj.put("d", "String2");
JSONObject json = new JSONObject(obj);
download this library:
https://github.com/douglascrockford/JSON-java
save all the files in a new package in your project
instead of using org.json.JSONObject use your.package.JSONObject which you added from the downloaded library.
now open JSONObject.java file and change HashMap() to LinkedHashMap() in the constructor
public JSONObject(Map map)
This will make the JSONObject store data in the order you entered the values using put.
You can't maintain the order of JSONObject response, because it is itself mentioned in the documents.
An object is an unordered set of name/value pairs.
I had faced the same problem and then i need to use either the gson library or to make json by ur logic..
An object is an unordered set of name/value pairs.
Best of luck.!
I am trying to create an array like this ["stringone","stringtwo"] and pass it to the webserver. When I tried doing making it with a string array like String[]={"stringone","stringtwo"] it passed in something weird {"userids":"[Ljava.lang.String;#406fe4b8"} how should I be constructing my JSON array if not by using string arrays?
Thanks
If you want to create JSONArray from List or array, you can use constructor, which takes Collection:
String[] data = {"stringone", "stringtwo"};
JSONArray json = new JSONArray(Arrays.asList(data));
The easiest way is to create a JSONArray object and use the put method(s) to add any Strings you want. To output the result, just use the toString() method.