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!