简体   繁体   中英

How to loop through json array of json object in java

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

Verify your JSON too and check below code. 

public class MyTest {

    public static void main(String[] args) throws JSONException {
        String str = "{\r\n" + 
                "   \"diagram\": [{\r\n" + 
                "           \"size\": {\r\n" + 
                "               \"width\": 30,\r\n" + 
                "               \"height\": 20\r\n" + 
                "           },\r\n" + 
                "           \"color\": \"blue\",\r\n" + 
                "           \"id\": 1\r\n" + 
                "       },\r\n" + 
                "       {\r\n" + 
                "           \"color\": \"red\",\r\n" + 
                "           \"id\": 2\r\n" + 
                "       },\r\n" + 
                "       {\r\n" + 
                "           \"size\": {\r\n" + 
                "               \"height\": 30\r\n" + 
                "           },\r\n" + 
                "           \"id\": 3\r\n" + 
                "       }\r\n" + 
                "   ]\r\n" + 
                "}";

        JSONObject jo = new JSONObject(str);
        final JSONArray geodata = jo.getJSONArray("diagram");
        int arrLength = geodata.length();
        for(int i = 0; i< arrLength;i++) {
            jo  = geodata.getJSONObject(i);
            System.out.println(jo.get("id"));
        }
    }
}

Your json format is wrong. You can always validate your json format using tools online

Correct json format

{  
   "diagram":[  
      {  
         "size":{  
            "width":30,
            "height":20
         },
         "color":"blue",
         "id":1
      },
      {  
         "color":"red",
         "id":2
      },
      {  
         "size":{  
            "height":30
         },
         "id":3
      }
   ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM