我正在尝试制作一个使用位于此处的 MLB 信息 API 的程序。我可以检索有关播放器的所有信息,但似乎无法使用 toString() 方法从 json 响应中提取更具体的信息。回复:{"search_player_all":{"copyRight":" Copyright 2019 MLB Advanced Media, L.P. Use of any content on this page acknowledges agreement to the terms posted here http://gdx.mlb.com/components/copyright.txt ","queryResults":{"totalSize":"1","created":"2019-05-28T12:34:47","row":{"college":"","pro_debut_date":"2012-09-05T00:00:00","birth_city":"Amsterdam","name_display_first_last":"Didi Gregorius","birth_date":"1990-02-18T00:00:00","height_inches":"3","team_id":"147","birth_state":"","name_last":"Gregorius","active_sw":"Y","birth_country":"Netherlands","bats":"L","player_id":"544369","service_years":"","name_display_last_first":"Gregorius, Didi","name_first":"Didi","league":"AL","weight":"205","name_use":"Didi","sport_code":"mlb","throws":"R","high_school":"","team_code":"nya","team_full":"New York Yankees","team_abbrev":"NYY","height_feet":"6","position":"SS","name_display_roster":"Gregorius","position_id":"6"}}}}我处理json的代码:JSONObject responsejson = response.getBody().getObject();String test = responsejson.getString("copyRight");注意:我正在使用 org.json.JSONObject 来处理。我希望它会返回:Copyright 2019 MLB Advanced Media, L.P. Use of any content on this page acknowledges agreement to the terms posted here http://gdx.mlb.com/components/copyright.txt 但我只是得到:Exception in thread "main" org.json.JSONException: JSONObject["copyRight"] not found.任何帮助表示赞赏。编辑:正如其他人所指出的,copyRight 不是顶级条目,但是,当我尝试 getString("search_player_all") 时,出现此错误:Exception in thread "main" org.json.JSONException: JSONObject["search_player_all"] not a string.解决方案:我使用 .getJSONObject() 向下导航到行级别。感谢您的帮助
1 回答
慕码人8056858
TA贡献1803条经验 获得超6个赞
您的顶级对象没有copyRight属性,它有一个search_player_all属性,其值为具有属性的对象copyRight。你需要先得到它:
JSONObject responsejson = response.getBody().getObject();
JSONObject search_player_all = responsejson.getJSONObject("search_player_all"); // Or maybe .getObject("search_player_all") with the lib you're using...?
String test = search_player_all.getString("copyRight");
添加回答
举报
0/150
提交
取消
