2015-09-22 31 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']等等。 –