![](/img/trans.png)
[英]Android JSON Exception - JSONArray cannot be converted to JSONObject
[英]Json - JSONArray cannot be converted to JSONObject
我是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
問題是toJSONArray
返回值數組。 它只有1個元素industryList
值。 這是數組本身。 因此,您需要迭代拋出valArray.getJSONArray(0)
而不是通過valArray
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.