I am trying to loop through the json file and find the value of particular json object. Here is my sample json:
{
"diagram":[
{"size":{"width":30,"height":20},"color":"blue","id":1},
{"color":"red","id":2},
{"size:{"height":30}", "id":3}
]
}
What i want to do is to iterate through the file and find the "id" element.
I used below code to convert the JsonFile into JsonObject and to get the value of "diagram" object
JSONArray jsonArray = new JSONArray();
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("D:/test.json"));
JSONObject jsonObj = (JSONObject) obj;
for(Iterator iterator = jsonObj.keySet().iterator(); iterator.hasNext();) {
String diagramKey = (String) iterator.next();
jsonArray.put(jsonObj.get(diagramKey));
}
With the above code i was able to get the value of diagram object and i have put that into the jsonArray
When i am trying to print the array object i am getting output as
[[
{"size":{"width":30,"height":20},"color":"blue","id":1},
{"color":"red","id":2},
{"size:{"height":30}", "id":3}
]]
and the jsonArray length is coming as 1.
How to loop through the above jsonArray and find the id of each individual element