Json 如何修复'AttributeError:'dict'对象没有属性…'python断言中的错误
我正在使用request模块和python assert关键字设置一个请求断言,但是 AttributeError:“dict”对象没有“documentation\u url”属性 当我尝试在json响应中断言字符串时。如何在json响应中断言某些内容,当条件为true时,它应该打印出一些内容Json 如何修复'AttributeError:'dict'对象没有属性…'python断言中的错误,json,python-3.x,assert,Json,Python 3.x,Assert,我正在使用request模块和python assert关键字设置一个请求断言,但是 AttributeError:“dict”对象没有“documentation\u url”属性 当我尝试在json响应中断言字符串时。如何在json响应中断言某些内容,当条件为true时,它应该打印出一些内容 import requests import pprint URL = 'https://github.com/timeline.json' def get_github_json(URL):
import requests
import pprint
URL = 'https://github.com/timeline.json'
def get_github_json(URL):
response = requests.get(URL).json()
return response
assert get_github_json(URL).documentation_url == 'https://developer.github.com/v3/activity/events/#list-public-events'
json响应如下所示:
{'documentation_url': 'https://developer.github.com/v3/activity/events/#list-public-events',
'message': 'Hello there, wayfaring stranger. If you’re reading this then you '
'probably didn’t see our blog post a couple of years back '
'announcing that this API would go away: http://git.io/17AROg Fear '
'not, you should be able to get what you need from the shiny new '
'Events API instead.'
}
您好,因为它是一个字典,所以您必须使用键获取值 我们知道response是一个字典,所以在这种情况下,当您需要文档\u url中的值时,我们需要这样做:
def get_github_json(url):
response = requests.get(url).json()
return response
assert get_github_json(url)['documentation_url'] # <---- your are getting the value by giving the key
如果您尝试打印响应['documentation\u url'],则会得到以下结果:
https://developer.github.com/v3/activity/events/list-public-events 您好,因为它是一个字典,所以您必须使用键获取值 我们知道response是一个字典,所以在这种情况下,当您需要文档\u url中的值时,我们需要这样做:
def get_github_json(url):
response = requests.get(url).json()
return response
assert get_github_json(url)['documentation_url'] # <---- your are getting the value by giving the key
如果您尝试打印响应['documentation\u url'],则会得到以下结果:
https://developer.github.com/v3/activity/events/list-public-events