J.vik
J.vik

Reputation: 123

How to fetch the value from Json array having nested data using Java code

I have to fetch the value from array "machines" having nested data and need to fetch for "value1": "Need to fetch this value". Tried a lot of code but still not able to fetch the same.

{
   "name":"anyname",
   "machines":[
      {
         "id":"771760",
         "type":"general",
         "properties":{
            "value1":"1",
            "value2":"2"
         }
      },
      {
         "id":"341256",
         "type":"general",
         "properties":{
            "value1":"Need to fetch this value"
         }
      },
      {
         "id":"341256",
         "type":"general",
         "properties":{
            "value1":"1",
            "value2":"2"
         }
      }
   ]
}

Tried using JsonObject and JsonArray, Still not worked

public String getValueForAnyKeyHavingNestedObjects(String jsonData,String outerObjectKey, String keyWhoseValueToFetch) throws JSONException {

JSONObject obj = new JSONObject(jsonData);

String value = String.valueOf(obj.getJSONObject(outerObjectKey).get(keyWhoseValueToFetch));
return value;
}

Upvotes: 0

Views: 65

Answers (1)

Andrei Sfat
Andrei Sfat

Reputation: 8606

So you have your jsonData which you need to put in a JSONObject.

You need to extract the machines, which is an array using getJSONArray("machines").

After that, you want to iterate over each machine and convert the machine into another JSONObject.

In order to take the value1, you just do a normal get("value1").

Full example here:

public static void main(String[] args) {
        String jsonData = "{\n"
                + "   \"name\":\"anyname\",\n"
                + "   \"machines\":[\n"
                + "      {\n"
                + "         \"id\":\"771760\",\n"
                + "         \"type\":\"general\",\n"
                + "         \"properties\":{\n"
                + "            \"value1\":\"1\",\n"
                + "            \"value2\":\"2\"\n"
                + "         }\n"
                + "      },\n"
                + "      {\n"
                + "         \"id\":\"341256\",\n"
                + "         \"type\":\"general\",\n"
                + "         \"properties\":{\n"
                + "            \"value1\":\"Need to fetch this value\"\n"
                + "         }\n"
                + "      },\n"
                + "      {\n"
                + "         \"id\":\"341256\",\n"
                + "         \"type\":\"general\",\n"
                + "         \"properties\":{\n"
                + "            \"value1\":\"1\",\n"
                + "            \"value2\":\"2\"\n"
                + "         }\n"
                + "      }\n"
                + "   ]\n"
                + "}";

        final JSONObject jsonObject = new JSONObject(jsonData);
        final JSONArray machines = jsonObject.getJSONArray("machines");
        for (int i = 0; i < machines.length(); i++) {
            final JSONObject machine = machines.getJSONObject(i);
            final JSONObject properties = machine.getJSONObject("properties");
            System.out.println(properties.get("value1"));
        }
    }

Result:

1

Need to fetch this value

1

Upvotes: 3

Related Questions