java - Iterate Array of JSON objects -


i using org.json.simple library construct jsonarray of jsonobject. structure looks like

c= [   {     "name":"test",     "age":1   },   {    "name":"test",    "age":1    } ] 

to iterate array in java, tried

for (int = 0; < c.size(); i++) {     jsonobject obj = (jsonobject) c.get(i);     system.out.println(obj.get("name"));         } 

it printed null, when tried print obj.tostring, prints json string expected.

i using org.json.simple jar, cannot use methods defined org.json.jsonarray or org.json.jsonobject.

any ideas values object key?

you can iterate on jsonarray elements using iterator, this:

    //arr jsonarray here     iterator<object> iterator = arr.iterator();     while (iterator.hasnext()) {         object obj = iterator.next();         if(obj instanceof jsonobject) {              system.out.println(obj.get("name"));         }     } 

it uses org.json.simple.jsonobject , org.json.simple.jsonarray.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -