即使密钥存在,JSONObject [" keyName"]在解析Json数据时未找到异常

时间:2017-09-21 22:24:31

标签: java json jsonexception

我有一个返回JSON数据的代码。我需要从它中选择某些值,但它会在某些键成功时抛出异常。

这是JSON数据

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

以下是我尝试从中选择值的方法

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"));           
            }

代码抛出异常 org.json.JSONException:JSONObject [&#34; Name&#34;]找不到。 虽然它能够读取 DateTimeLastModified 值。

请帮我解决此问题。

1 个答案:

答案 0 :(得分:0)

这应该对你有用,修改你的代码如下:

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"));           
        }

尝试并确认我是否有效