0

I have a code which returns JSON data. I need to pick certain values from it but it throws Exception for some keys while some are successful.

Here is the JSON data

{"value":[{"Name":"abc.txt","DateTimeLastModified":"2017-09-21T20:11:04Z","IsInline":false,"ContentBytes":"some byte data","IsContactPhoto":false}]}

Here is how I am trying to pick the values from it

JSONObject jsonObject = response.getBody().getObject();
    JSONArray tsmresponse = (JSONArray) jsonObject.get("value");
    for(int i=0; i<tsmresponse.length(); i++){
        System.out.println("Name:: "+tsmresponse.getJSONObject(i).getString("Name"));           
            }

The code throws Exception org.json.JSONException: JSONObject["Name"] not found. while it is able to read DateTimeLastModified value.

Please help me to resolve this issue.

rats
  • 13
  • 1
  • 4
  • 1
    I think it is time for you to learn how to debug. – rmlan Sep 21 '17 at 22:27
  • 1
    Aye sir..but I was looking for something more helpful in comment. Like telling me if I am doing something wrong :) – rats Sep 21 '17 at 22:39
  • I ran the code you posted against the JSON string you posted, and it came back fine with the output as expected. This means that what you have posted is not telling the whole story, even if you think it is. I cannot tell you from here if you have "done something wrong", but a debugger could certainly help you determine that. – rmlan Sep 21 '17 at 22:54

1 Answers1

0

this should work for you, modify your code as follow:

JSONObject jsonObject = response.getBody().getObject();
JSONArray tsmresponse = jsonObject.getJSONArray("value");//here is your modification
for(int i=0; i<tsmresponse.length(); i++){
    System.out.println("Name:: "+tsmresponse.getJSONObject(i).getString("Name"));           
        }

try it and confirm me if this works

Freddy Sop
  • 117
  • 1
  • 1
  • 8