getJSONObject error: JSONObject[ "..." ] 不是 JSONObject



我试图解析JSON字符串,但在尝试获取嵌套对象时遇到错误:

JSONObject jsonObject = new JSONObject(jsonString);
System.out.println(jsonObject);
System.out.println(jsonObject.keySet());
System.out.println(jsonObject.getJSONObject("matches"));

下面是控制台中的输出。据我所见,JSON是有效的,因为jsonObject是在没有错误的情况下创建的。但当我试图获得"匹配"时,它会抛出一个错误。我已经将我的代码与教程进行了比较,但我看不出问题出在哪里:

{"matches":[{"id":233028,"awayTeam":{...
[matches, count, filters, competition]
Error in client: JSONObject["matches"] is not a JSONObject.

我做错了什么?如果需要,很乐意提供任何进一步的信息。

matches数组,而不是对象。使用getJSONArray:

System.out.println(jsonObject.getJSONArray("matches"));

(或者更有用的:

System.out.println(Arrays.deepToString(jsonObject.getJSONArray("matches")));

因为数组上的System.out.println本身并没有真正显示有用的信息。(

最新更新