![](/img/trans.png)
[英]any workaround to easily validate nested JSON response in python?
[英]is there any way to validate json response in python?
我正在尝试验证python中的json,这是来自url击中的响应。 我如何在python中验证它,以确保此响应仅是json?
def SLRCLDQR011():
try:
url = 'http://someurl.com/q='
query = 'running shoes'
resp = requests.get(url + query)
except requests.exceptions.ConnectionError as e:
print 'Domain Name is not reachable'
except:
print "testcase {0} failed ".format(funcName)
logfile.write("testcase {0} failed ".format(funcName))
print "Unexpected error:", sys.exc_info()[0]
raise
响应对象具有方法json
,该方法将尝试将响应主体解析为json
。
resp = get("https://httpbin.org/ip")
resp.json()
{'origin': '81.89.63.129'}
对于非json响应,它将抛出ValueError
:
resp = get("https://httpbin.org/")
resp.json()
...
...
ValueError: Expecting value: line 1 column 1 (char 0)
如果你想验证什么是在JSON使用妖娆或jsonschema 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.