Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android JSONArray无法解析_Android - Fatal编程技术网

Android JSONArray无法解析

Android JSONArray无法解析,android,Android,对于这个简单的问题,我感到很困惑,因为我无法将数据从JSONObject获取到JSONArray。在此行生成异常: JSONArray contacts = jsonObj.getJSONArray(jsonStr); 异常错误 Json parsing error: No value for {"image":"John","description":30,"city":"New York"} 对象中没有数组 删除for circle并按如下方式解析对象: String id= json

对于这个简单的问题,我感到很困惑,因为我无法将数据从JSONObject获取到JSONArray。在此行生成异常:

JSONArray contacts = jsonObj.getJSONArray(jsonStr);
异常错误

 Json parsing error: No value for {"image":"John","description":30,"city":"New York"}

对象中没有数组

删除for circle并按如下方式解析对象:

String id= jsonObj.getString("image");
Json数组以[开头,以]结尾

在您的回答中,您没有这些符号,因此您没有Json数组

要解决这个问题,只需使用Farshad所说的Json对象:

String id = jsonObj.getString("image");
解决方案:

首先,删除行JsonArray,因为Json不是数组,所以也删除for循环。。因为它不是数组,所以不需要for循环

其次,在代码中进行以下更改:

JSONObject jsonobj = new JSONObject(jsonstr);

String image = jsonObj.getString("image");
int desc = jsonObj.getInt("description")
String city = jsonObj.getString("city");
现在,您可以在任何地方使用字符串。删除所有其他内容,因为您不会得到所有这些内容作为响应


希望有帮助。

首先,它不是数组,只是一个Json对象。。第二,您将获得3个元素,但在for.loop中请求5个元素