Pablo Fernández
Pablo Fernández

Reputation: 51

Retrieving nested arrays values with JSON Java

I'm struggling to retrieve some values from a JSON file formatted like this one:

{
  "search": {
    "entry": [
      {
        "found": "identity=9454532,l=big,ton=grand,k=molvi",
        "attribute": [
          {
            "name": "firstname",
            "value": [
              "Lucas"
            ]
          },
          {
            "name": "lastname",
            "value": [
              "Brandon"
            ]
          }
        ]
      }
    ],
    "return": {
      "code": 0,
      "message": "Success",
      "count": 1
    }
  }
}

I have tried different approaches (json, gson, jayway-JsonPath) but I don't manage to get the values from the "attribute" array, only those from the first array. I don't know how to specify that "attribute" is an JSONArray and not a JSONObject or how to set the proper path to it. This is the last code I was playing with which stops when it founds an array:

public void String nameObtain (String email) throws IOException{

String link = "http://jsonfile/" + email;

    JSONObject json = readJsonFromUrl(link);        
    JSONObject rootObject = json.getJSONObject("search");
    JSONArray firstArray = rootObject.getJSONArray("entry");

for (int i = 0, size = firstArray.length(); i < size; i++) {
    JSONObject objectInArray = firstArray.getJSONObject(i);

    String[] elementNames = JSONObject.getNames(objectInArray);
    System.out.printf("%d ELEMENTS IN CURRENT OBJECT:\n", elementNames.length);
    for (String elementName : elementNames) {
        String value = objectInArray.getString(elementName);
        System.out.printf("name=%s, value=%s\n", elementName, value);
    }

}

}

What I would like to do is to get the values Lucas or Brandon. Any help will be much appreciated!

Upvotes: 3

Views: 19102

Answers (1)

Rishal
Rishal

Reputation: 1538

Libraries used:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

Check the below code and step wise parsing

    JSONObject search = (JSONObject) jsonObject.get("search");//1
    JSONArray entry = (JSONArray) search.get("entry");//2
    for (int i = 0; i < entry.size(); i++) {
        JSONObject jsonObject1 = (JSONObject) entry.get(i);//3
        JSONArray jsonarray1 = (JSONArray) jsonObject1.get("attribute");//4
        for (int j = 0; j < jsonarray1.size(); j++) {
            System.out.println(((JSONObject) jsonarray1.get(j)).get(
                    "value").toString());//5

        }

    }

It will give the values mentioned step wise:

1) {"entry":[{"found":"identity=9454532,l=big,ton=grand,k=molvi","attribute":[{"name":"firstname","value":["Lucas"]},{"name":"lastname","value":["Brandon"]}]}],"return":{"code":0,"count":1,"message":"Success"}}

2) [{"found":"identity=9454532,l=big,ton=grand,k=molvi","attribute":[{"name":"firstname","value":["Lucas"]},{"name":"lastname","value":["Brandon"]}]}]

3) {"found":"identity=9454532,l=big,ton=grand,k=molvi","attribute":[{"name":"firstname","value":["Lucas"]},{"name":"lastname","value":["Brandon"]}]}

4) [{"name":"firstname","value":["Lucas"]},{"name":"lastname","value":["Brandon"]}]

5) ["Lucas"] and ["Brandon"]

So basically you have to take care of JSONObject and JSONArray respectively and do the parsing accordingly.

Upvotes: 7

Related Questions