从JSONArray获取JSONObject

时间:2018-11-15 12:31:31

标签: java arrays json org.json

for(int i = 0; i < ja.length(); i++){
            JSONObject jo = ja.getJSONObject(i);
            Resources.show(jo);
}

在上面的代码中,我想从json数组中获取json对象。但是我遇到了错误

org.json.JSONException: JSONArray[0] is not a JSONObject.
at org.json.JSONArray.getJSONObject(JSONArray.java:343)
at webutilities.Try.main(JSON.java:50)

成功建立(总时间:0秒)

还有什么方法可以从json数组中获取json对象。

1 个答案:

答案 0 :(得分:0)

尝试

System.out.println(ja.get(i).getClass().toString());

找出您的JSONArray中存在哪种对象。

例如如果您有字符串,只需使用:

ja.getString(i)