How to get String value from JSON Array?

1.9k views Asked by At

I know this has been asked many times but none of the solution provided is working in my case.

Here is my JSON which I am getting as a response from a webservice

{
"size": 1,
"_links": {
"context": "XXXX",
"self": "XXXXXX",
"base": "XXXXXX"
},
"start": 0,
"limit": 50,
"results": [
 {
  "container": {
    "extensions": {
      "position": "none"
    },
    "_links": {
      "webui": "XXXX",
      "tinyui": "XXXX",
      "self": "XXXXXX"
    },
    "id": "XXXXX",
    "type": "page",
    "title": "XXXXX",
    "_expandable": {
      "container": "XXXXX",
      "metadata": "",
      "operations": "",
      "children": "XXXX/child",
      "history": "XXXX/history",
      "ancestors": "",
      "body": "",
      "version": "",
      "descendants": "XXXXX/descendant",
      "space": "XXXXX"
    },
    "status": "current"
  },
  "metadata": {
    "mediaType": "text/plain",
    "comment": ""
  },
  "extensions": {
    "fileSize": 8,
    "mediaType": "text/plain",
    "comment": ""
  },
  "_links": {
    "download": "/download/attachments/XXXXX/MyData1.txt?version=1&modificationDate=1483652732000&api=v2",
    "webui": "XXXXX",
    "self": "XXXXX"
  },
  "id": "attXXXXXX",
  "type": "attachment",
  "title": "MyData1.txt",
  "_expandable": {
    "operations": "",
    "children": "XXXX",
    "history": "XXXXX",
    "ancestors": "",
    "body": "",
    "descendants": "XXXXX",
    "space": "XXXXX"
  },
  "version": {
    "number": 1,
    "minorEdit": false,
    "by": {
      "profilePicture": {
        "path": "XXXXX",
        "isDefault": true,
        "width": 48,
        "height": 48
      },
      "displayName": "XXXXX",
      "type": "known",
      "userKey": "XXXXX",
      "username": "XXXXX"
    },
    "message": "",
    "when": "2017-01-05T16:45:32.000-05:00"
  },
  "status": "current"
}
]
}

I need to find the value of the id which is attXXXXXX and here is my code.

JSONObject page = new JSONObject(pageObj); 
JSONArray jsonArray= page.getJSONArray("results");

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
}

But the above loop just runs once as the lenght of the jsonArray is 1. All the solution provided in other posts are the same i.e. iterating over the json array. Am I missing something ? Suggestion ?

2

There are 2 answers

3
GrabNewTech On

Your JSON string is having only one value in "results" object. So your array size is returning as 1. Which is correct.

If you are expecting more than one array then check the server side code where JSON is built.

Refer to below image for more understanding.

enter image description here

I didn't tested it. Can you try this one. If you got the solution plz up-vote.

JSONObject page = new JSONObject(pageObj); 
JSONArray jsonArray= page.getJSONArray("results");

for (int i = 0; i < jsonArray.length(); i++) {
   JSONObject explrObject = jsonArray.getJSONObject(i);
   explrObject.getString("id")
}
0
Naseem On

Ok, So I was able to get the value from the response using Rest Assured API. It's an easy API to get the value from JSON Array. Basically I have to use the following command to get the value

response.path("results.id");