javajsonjson-simple

How can I get multiple json data from json file one by one?


I am trying to get specific value from json file. Here is my json file.

{
  "books": ["book1","book2","book3","book4"],
  "set_with_ratio": {
    "tweaks_es": "7",
    "ratio": {
      "brand_defined_sets_ratio": {
        "default": {
          "desktop": {
            "16": "85",
            "11": "95",
            "19": "10",
            "12": {
              "2": "50"
            }
          }
        },
        "rentals": {
          "desktop": {
            "16": "75",
            "11": "45",
            "12": {
              "2": "10"
            }
          }
        }
      }
    }
  }
}

How can I get books array elements one by one? I tried this.

JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonData);
JSONArray jsonArray = (JSONArray) jsonObject.get("books");
//Iterating the images of the array
Iterator<Object> iterator = jsonArray.iterator();
while(iterator.hasNext()) {
 System.out.println(iterator.next());
 }

From this I got this error java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray how can I get data for x.set_with_ratio.ratio.brand_defined_sets_ratio.default.desktop.11 this json path. I am trying this code in Java.

JSONParser jsonParser = new JSONParser();
        try {
            //Parsing the contents of the JSON file
            JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("sample.json"));
            String id = jsonObject.get("set_with_ratio").toString();
            System.out.println(id);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

Here I get the total json file but cannot manage to get expected result.


Solution

  • Try this

    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    import org.junit.Test;
    
    
    public class JavaTest {
        @Test
        public void name() {
            JSONParser jsonParser = new JSONParser();
            try {
                //Parsing the contents of the JSON file
                JSONObject jsonObject = (JSONObject) jsonParser.parse("{\n" +
                        "  \"set_with_ratio\": {\n" +
                        "    \"tweaks_es\": \"7\",\n" +
                        "    \"ratio\": {\n" +
                        "      \"brand_defined_sets_ratio\": {\n" +
                        "        \"default\": {\n" +
                        "          \"desktop\": {\n" +
                        "            \"16\": \"85\",\n" +
                        "            \"11\": \"95\",\n" +
                        "            \"19\": \"10\",\n" +
                        "            \"12\": {\n" +
                        "              \"2\": \"50\"\n" +
                        "            }\n" +
                        "          }\n" +
                        "        },\n" +
                        "        \"rentals\": {\n" +
                        "          \"desktop\": {\n" +
                        "            \"16\": \"75\",\n" +
                        "            \"11\": \"45\",\n" +
                        "            \"12\": {\n" +
                        "              \"2\": \"10\"\n" +
                        "            }\n" +
                        "          }\n" +
                        "        }\n" +
                        "      }\n" +
                        "    }\n" +
                        "  }\n" +
                        "}");
                String id = ((JSONObject) ((JSONObject) ((JSONObject) ((JSONObject) ((JSONObject) jsonObject.get("set_with_ratio"))
                        .get("ratio"))
                        .get("brand_defined_sets_ratio"))
                        .get("default"))
                        .get("desktop"))
                        .get("11" +
                        "").toString();
                System.out.println(id);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    

    Result is 95