0

我是 JSON 新手。我正在尝试读取 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"));
}

错误:

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

1 回答 1

1

问题是toJSONArray返回值数组。而且它只有 1 个元素 -industryList值。这是数组本身。所以你需要迭代 throw valArray.getJSONArray(0)not throughvalArray

于 2012-08-07T08:55:05.707 回答