我是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
答案 0 :(得分:1)
问题是toJSONArray
返回值数组。它只有1个元素 - industryList
值。这是阵列本身。所以你需要迭代抛出valArray.getJSONArray(0)
而不是valArray