0

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

2
  • In your input the diagram is an array which contains multiple objects. In your output you have an array of those arrays. It looks like you're handling a single diagram so far, so your output array contains a single array. You could loop over that inner array and you'd find the three ids of the objects it contains Commented May 16, 2019 at 9:40
  • Your json is not correctly formatted Commented May 16, 2019 at 9:48

2 Answers 2

1
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"));
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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
      }
   ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.