Iterate Array of JSON objects

I am using org.json.simple library to construct JSONArray of JSONObject. so my structure looks like ,

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

to iterate the array in java, i tried

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

It printed null, but when tried to print the obj.toString it prints the json string as expected.

Any ideas to get the values from the object with their key ?

P.N : I am using org.json.simple jar, so cant use the methods defined org.json.JSONArray or org.json.JSONObject


Source: json

Leave a Reply