3

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!

Dut A.
  • 1,029
  • 11
  • 22
Pablo Fernández
  • 51
  • 1
  • 1
  • 7
  • So you want to get a JSON array but are using `getString`? Just use the simple `get` method if you don't want to specify which type you expect. – Sotirios Delimanolis Jul 25 '16 at 18:27
  • Or use `getJSONArray` for those you know are JSON arrays. – Sotirios Delimanolis Jul 25 '16 at 18:28
  • Are you able to get values back for the "entry" array? if so, then it should be similar to the "attribute" array. `getJSONArray` method is all you need. whatever comes back from it can be treated as a JSONObject and iterated over. – Dut A. Jul 25 '16 at 18:37
  • @SotiriosDelimanolis If I just try with json.get("firstname"); the object is not found. What I am unable to achieve is to set the proper path to get to that array. – Pablo Fernández Jul 25 '16 at 18:39
  • 1
    There is no _proper path_. You get each member individually until you reach the most nested member you're looking for. – Sotirios Delimanolis Jul 25 '16 at 18:45
  • So that's what I don't know how to do. If, for instance, like this: `code`JSONObject rootObject = json.getJSONObject("search"); JSONArray firstArray = json.getJSONArray("value");`code` JSON does not find it: org.json.JSONException: JSONObject["value"] not found. – Pablo Fernández Jul 25 '16 at 18:55
  • There's no `value` array in `search`. There's an array named `entry`, which has one object, which has an array named `attribute` which has an object, which has an array named `value`. – Sotirios Delimanolis Jul 25 '16 at 18:58
  • Use a library which supports JSON Pointer. – fge Jul 25 '16 at 19:08

1 Answers1

7

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.

Rishal
  • 1,480
  • 1
  • 11
  • 19
  • Thanks @Rishal, finally some light!! – Pablo Fernández Jul 25 '16 at 19:29
  • I have same problem means I need to update that inner values , problem is updating list of values directly to the inner object ,in that position are coming 0,1,2,0,1,3,4,5,0 like this if I'm trying to update all arraylist of values in that loop same data pasting that positions @Rishal – Venkatesh Mar 24 '19 at 02:05
  • the questions with details will be helpful like what exactly ypu are trying to do and what u r not able to do. – Rishal Mar 26 '19 at 04:31