2015-09-22 33 views
1

我一直在努力,當我在網址欄中輸入使用API​​有以下回應:API/JSON值錯誤

{ 
    "resource": "playerdashptshotlog", 
    "parameters": { 
     "LeagueID": "00", 
     "Season": "2014-15", 
     "SeasonType": "Regular Season", 
     "PlayerID": 202066, 
     "TeamID": 0, 
     "Outcome": null, 
     "Location": null, 
     "Month": 0, 
     "SeasonSegment": null, 
     "DateFrom": null, 
     "DateTo": null, 
     "OpponentTeamID": 0, 
     "VsConference": null, 
     "VsDivision": null, 
     "GameSegment": null, 
     "Period": 0, 
     "LastNGames": 0 
    }, 
    "resultSets": [ 

我的代碼如下:

import json, requests 
github_url = 'http:dsds 
parsed_input = json.loads(github_url) 
print (parameters.keys()) 
print (parameters['LeagueID']['Season']) 

我得到

Traceback (most recent call last): File "C:\Python34\Scripts\NBA API-JSON.py", line 27, in parsed_input = json.loads(github_url) File "C:\Python34\lib\json__init__.py", line 318, in loads return _default_decoder.decode(s) File "C:\Python34\lib\json\decoder.py", line 343, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode raise ValueError(errmsg("Expecting value", s, err.value)) from None ValueError: Expecting value: line 1 column 1 (char 0)

當我在Python27運行它,我得到這個錯誤:當我使用Python34說錯誤

Traceback (most recent call last): File "C:\Python27\Scripts\NBA API-JSON.py", line 27, in parsed_input = json.loads(github_url) File "C:\Python27\lib\json__init__.py", line 338, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded

我想弄清楚我做錯了什麼。我試圖用一個例子回答我發現在發現一個問題:

Parsing Multidimensional JSON Array

+0

你不能複製所有的迴應,因爲你粘貼的內容無效JSON,它以開放的結束'[ ' – Victory

+0

大約60頁的價值 –

回答

2

看起來你忘了取數據。嘗試:

github_url = 'http://whatever' 
r = requests.get(github_url) 
if r.status_code == 200: 
    parsed_input = json.loads(r.text) 

請求也可以解析JSON爲您提供:

parsed_input = r.json() 
+0

我怎樣才能打印某些領域,例如:TeamID,PlayerID等?這樣我可以稍後操縱這些值中的任何一個,就像表格格式一樣。 –

+0

@RossJohnson我不認爲這與你的問題有關。我相信這個答案可以回答你的問題。如果你認爲這樣做,那麼最好是接受它。我建議用更多的細節爲此創建另一個問題。 – V13

+0

json.loads()返回一個常規的python字典或列表,具體取決於輸入的JSON是什麼。在你的情況下做parsed_input ['parameters'] ['TeamID']等等。 –