简体   繁体   English

getJSONObject错误:JSONObject [“…”]不是JSONObject

[英]getJSONObject error: JSONObject[“…”] is not a JSONObject

I'm trying to parse a JSON string, but getting an error when trying to get a nested object: 我正在尝试解析JSON字符串,但是在尝试获取嵌套对象时遇到错误:

JSONObject jsonObject = new JSONObject(jsonString);

System.out.println(jsonObject);
System.out.println(jsonObject.keySet());
System.out.println(jsonObject.getJSONObject("matches"));

Below is the output in console. 以下是控制台中的输出。 As far as I can see, the JSON is valid as jsonObject is created without an error. 据我所知,JSON是有效的,因为创建JSONObject时没有错误。 But when I try to obtain "matches" it throws an error. 但是,当我尝试获取“匹配项”时,会引发错误。 I've compared my code with tutorials but I can't see what the issue out to be: 我已经将我的代码与教程进行了比较,但是我看不出问题出在哪里:

{"matches":[{"id":233028,"awayTeam":{...

[matches, count, filters, competition]

Error in client: JSONObject["matches"] is not a JSONObject.

Anything I'm doing wrong? 我做错了什么吗? Happy to provide any further info if needed. 如果需要,很高兴提供任何进一步的信息。

matches is an array , not an object. matches是一个数组 ,而不是一个对象。 Use getJSONArray : 使用getJSONArray

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

(Or more usefully: (或更有用的是:

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

since System.out.println on an array doesn't really show useful information on its own.) 因为数组上的System.out.println本身并不真正显示有用的信息。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM