简体   繁体   English

如何使用python在JSON中断言

[英]How to assert deeply in JSON with python

I am trigerring an API call, and the server response is in JSON format. 我正在触发一个API调用,并且服务器响应为JSON格式。 the response looks like this: 响应如下所示:

{
"status": 0, 
"not_passed": 1, 
"why": [
    {
        "code": 229, 
        "reason": "some reason", 
     }
]
}

I need to assert two thing. 我需要断言两件事。 Status and reason fro status I am using: 我正在使用的状态和原因

    r = requests.get_simple(url=Server.MY_SERVER, params=p)
data = json.loads(r.content)
assert data["status"] == 0

but it doesn't work for the 'reason', maybe because the 'reason' is deeper in the nested structure. 但这不适用于“原因”,可能是因为“原因”在嵌套结构中更深。 How can I fix this one? 我该如何解决?

assert data['why'][0]['reason'] == 'something'

当然,这假定data['why']存在,是一个列表,并包含一个dict作为其第一个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM