简体   繁体   English

Json-JSONArray无法转换为JSONObject

[英]Json - JSONArray cannot be converted to JSONObject

I'm new to JSON. 我是JSON的新手。 I'm trying to read the String in JSON but it throw me an error. 我正在尝试读取JSON中的字符串,但它给我抛出了一个错误。

String strResponse = "{"IndustryList" : [{"industryId" : "1","industryName" : "Bird Nest","industryStatus" : "0","industryDescription" : "Bird nest industry","industryIconFilepath" : "","industryIconFilename" : ""},{"industryId" : "2","industryName" : "Medicine","industryStatus" : "0","industryDescription" : "Medicine Products","industryIconFilepath" : "","industryIconFilename" : ""}]}"

JSONObject json_data = new JSONObject(strResponse);
JSONArray nameArray = json_data.names(); 
JSONArray valArray = json_data.toJSONArray(nameArray);
Log.i(MainActivity.class.getName(),
             "Number of entries " + valArray.length());
for (int i = 0; i < valArray.length(); i++) {
    JSONObject jsonObject = valArray.getJSONObject(i);
    Log.i(MainActivity.class.getName(), jsonObject.getString("text"));
}

Error: 错误:

at 0 of type org.json.JSONArray cannot be converted to JSONObject

The problem is that toJSONArray returns array of values. 问题是toJSONArray返回值数组。 And it has only 1 element - industryList value. 它只有1个元素industryList值。 Which is array itself. 这是数组本身。 So you need to iterate throw valArray.getJSONArray(0) not through valArray 因此,您需要迭代抛出valArray.getJSONArray(0)而不是通过valArray

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM