亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

即使從瀏覽器 api 調用復制粘貼,JSONObject.toString("variable")

即使從瀏覽器 api 調用復制粘貼,JSONObject.toString("variable")

元芳怎么了 2022-12-21 12:59:18
我正在嘗試制作一個使用位于此處的 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");


查看完整回答
反對 回復 2022-12-21
  • 1 回答
  • 0 關注
  • 99 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號