我正在嘗試制作一個使用位于此處的 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
提交
取消