This question already has an answer here:
- How to parse JSON in Java 31 answers
I have JSON object as follows:
member = "{interests : [{interestKey:Dogs}, {interestKey:Cats}]}";
In Java I want to parse the above jsonobject and store the values in an arraylist.
Can any one please provide me some code snippet through which i can achieve this.
Thanks
I'm assuming you want to store the interestKeys in a list.
Using the org.json library:
There are many JSON libraries available in Java.
The most notorious ones are: Jackson, GSON, Genson, FastJson and org.json.
There are typically three things one should look at for choosing any library:
Specifically for JSON libraries (and any serialization/deserialization libs), databinding is also usually of interest as it removes the need of writing boiler-plate code to pack/unpack the data.
For 1, see this benchmark: https://github.com/fabienrenaud/java-json-benchmark I did using JMH which compares (jackson, gson, genson, fastjson, org.json, jsonp) performance of serializers and deserializers using stream and databind APIs. For 2, you can find numerous examples on the Internet. The benchmark above can also be used as a source of examples...
Quick takeaway of the benchmark: Jackson performs 5 to 6 times better than org.json and more than twice better than GSON.
For your particular example, the following code decodes your json with jackson:
Let me know if you have any questions.
Thank you soooooooo much to CODE I can read any JSON file thanks to your code :) Now, I'm trying to organize all the elements by levels, for could use them!
I was working with Android reading a JSON from an URL and the only I had to change was the lines
Set<Object> set = jsonObject.keySet(); Iterator<Object> iterator = set.iterator();
for
Iterator<?> iterator = jsonObject.keys();
I share my implementation, to help someone:
1.) Create an arraylist of appropriate type, in this case i.e
String
2.) Create a
JSONObject
while passing your string toJSONObject
constructor as inputJSONObject
notation is represented by braces i.e{}
JSONArray
notation is represented by square brackets i.e[]
3.) Retrieve
JSONArray
fromJSONObject
(created at 2nd step) using"interests"
as index.4.) Traverse
JASONArray
using loops upto the length of array provided bylength()
function5.) Retrieve your
JSONObjects
fromJSONArray
usinggetJSONObject(index)
function6.) Fetch the data from
JSONObject
using index '"interestKey"'.Note :
JSON
parsing uses the escape sequence for special nested characters if the json response (usually from other JSON response APIs) contains quotes ("
) like thisshould be like this
so you can use
JSONParser
to achieve escaped sequence format for safety asCode :
or
Note : Sometime you may see some exceptions when the
values are not available in appropriate type or is there is no mapping key
so in those cases when you are not sure about the presence of value so useoptString
,optInt
,optBoolean
etc which will simply return the default value if it is not present and even try to convert value to int if it is of string type and vice-versa so Simply No null or NumberFormat exceptions at all in case of missing key or valueFrom docs
or To get empty string i.e
""
if no key found then simply useFurther reference to study