Python 如何打印带有请求和JSON的变量

Python 如何打印带有请求和JSON的变量,python,json,object,python-requests,nonetype,Python,Json,Object,Python Requests,Nonetype,我一直在编写一个从在线API获取信息的应用程序,我需要一些帮助 我正在使用请求,我当前的代码如下 myData = requests.get('theapiwebsitehere.com/thispartisworking') myRealData = myData.json() x = myRealData['data']['playerStatSummaries']['playerStatSummarySet']['maxRating'] print x 然后我得到这个错误 myRealD

我一直在编写一个从在线API获取信息的应用程序,我需要一些帮助

我正在使用请求,我当前的代码如下

myData = requests.get('theapiwebsitehere.com/thispartisworking')
myRealData = myData.json()
x = myRealData['data']['playerStatSummaries']['playerStatSummarySet']['maxRating']
print x
然后我得到这个错误

myRealData = myData.json()                                                                                                                      
TypeError: 'NoneType' object is not callable
我希望能够得到变量maxRating,并将其打印出来,但我似乎无法做到这一点


谢谢你的帮助。

首先,我的数据真的返回了什么吗

如果是,则可以尝试以下操作,而不是使用.json()函数

导入Json包并对文本使用Json loads函数

import json
newdata = json.loads(myData.text())

两件事,首先,确保您使用的是最新版本的
请求
(1.1.0);在以前的版本中,
json
不是一个方法,而是一个属性

>>> r = requests.get('https://api.github.com/users/burhankhalid')
>>> r.json['name']
u'Burhan Khalid'
>>> requests.__version__
'0.12.1'
在最新版本中:

>>> import requests
>>> requests.__version__
'1.1.0'
>>> r = requests.get('https://api.github.com/users/burhankhalid')
>>> r.json()['name']
u'Burhan Khalid'
>>> r.json
<bound method Response.json of <Response [200]>>
总之:

  • 升级您的
    请求
    pip安装-U请求
  • 确保您的URL返回有效的JSON

  • 你能在.get()之后“打印我的数据”吗?我猜get没有返回您认为应该返回的(似乎没有返回)。对请求不太熟悉。您还可以发布一些真实的代码(该URL不起作用)。请求已经将json解码为python对象,这是喜欢它的另一个原因。是的,但服务有时返回带有错误标题的json。我发现这种第二种方法是一种很好的回退方法。旧版本的请求是我的问题。
    >>> r = requests.get('http://www.google.com/')
    >>> r.json # Note, this returns None
    >>> r.json()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'NoneType' object is not callable