java - JSON has multiple Objects with the same name -


i have small function makes json api request , returns string postexecute:

public class fetch extends asynctask<url, void, string> {     @override     protected string doinbackground(url... urls) {         url search = urls[0];         string results = null;         try {             results = getresponse(search);         } catch (ioexception e) {             e.printstacktrace();         }         return results;     } 

now want specific json objects, have multiple of same objects:

[     {         "a1": {             "about": {                 "ids": [19]             }, ,             "value": 1         },         "a2": {             "about": {                 "ids": [19]             },             "value": 2         }     },     {         "a1": {             "about": {                 "ids": [20]             }, ,             "value": 3         },         "a2": {             "about": {                 "ids": [20]             },             "value": 4         }     } ] 

how do that?

you have set of map objects in array there.

take @ in pretty json editor: http://jsoneditoronline.org/?id=78f6765cb52e5403d01a8feead392611

you'll access array element 0 or 1 first, access map object.

secondobject = myjsonarray.getjsonobject(1) a2 = secondobject.getjsonobject("a2") thisisfour = a2.getint("value") 

or strung together:

thisisfour = myjsonarray.getjsonobject(1).getjsonobject("a2").getint("value") 

relevant javadoc references:

http://docs.oracle.com/javaee/7/api/javax/json/jsonobject.html http://docs.oracle.com/javaee/7/api/javax/json/jsonarray.html


Comments