获取第三或第四级JSON密钥的值

时间:2019-04-04 13:58:49

标签: python json

我试图根据Python中第三或第四级JSON密钥的值编写python测试,但不知道如何做。

这是JSON代码的示例:

{  
    "status":"Success",
    "firstID":"12345",
    "errorDetails":{  
        "message":null,
        "additionalinfo":{  
        }
    },
    "randomID":"678910",
    "response":[  
        {  
            "aid":"123456",
            "outcome":"Good",
            "stausMessage":"success",
            "additionalInfo":{  
                "successes":[  
                    {  
                        "aid":"097c9e28",
                        "additionalProperties":{  
                            "details":[  
                                "Successful call"
                            ]
                        }
                    },
                    {  
                        "aid":"097c9e28",
                        "additionalProperties":{  
                            "details":[  
                                "Successful call"
                            ]
                        }
                    },
                    {  
                        "aid":"097c9e28",
                        "additionalProperties":{  
                            "details":[  
                                "Successful call"
                            ]
                        }
                    },
                    {  
                        "aid":"097c9e28",
                        "additionalProperties":{  
                            "details":[  
                                "Successful call"
                            ]
                        }
                    }
                ],
                "errors":[  

                ]
            }
        }
    ],
    "additionalInfo":{  

    }
}

此json是称为响应的REST服务响应的一部分。我可以使用以下代码访问第二级键:

        a_id = int(self.response.json()['response'][0]['aid'])
        assert_that(aid).is_between(123450, 123499)

但是我不知道如何到达名为“ aid”的低级键,该键等于097c9e28。

3 个答案:

答案 0 :(得分:1)

您只需要进一步钻取即可,例如,第四级位于['additionalInfo']['success']下,要访问它,您需要使用['response'][0]['additionalInfo']['successes'][0]['aid'],其中0是索引。

例如['response'][0]['additionalInfo']['successes'][0]为您提供以下结果:

{'aid': '097c9e28', 'additionalProperties': {'details': ['Successful call']}}

答案 1 :(得分:1)

相当不错,但是如果您想要单线,那就是这样:

a_id = int(self.response.json()["response"][0]["additionalInfo"]["successes"][0]["aid"])

答案 2 :(得分:0)

您可以尝试这样:

import json
resp = json.loads("""
{  
    "status":"Success",
    "firstID":"12345",
    "errorDetails":{  
        "message":null,
        "additionalinfo":{  
        }
    },
    "randomID":"678910",
    "response":[  
        {  
            "aid":"123456",
            "outcome":"Good",
            "stausMessage":"success",
            "additionalInfo":{  
                "successes":[  
                    {  
                        "aid":"097c9e28",
                        "additionalProperties":{  
                            "details":[  
                                "Successful call"
                            ]
                        }
                    },
                    {  
                        "aid":"097c9e28",
                        "additionalProperties":{  
                            "details":[  
                                "Successful call"
                            ]
                        }
                    },
                    {  
                        "aid":"097c9e28",
                        "additionalProperties":{  
                            "details":[  
                                "Successful call"
                            ]
                        }
                    },
                    {  
                        "aid":"097c9e28",
                        "additionalProperties":{  
                            "details":[  
                                "Successful call"
                            ]
                        }
                    }
                ],
                "errors":[  

                ]
            }
        }
    ],
    "additionalInfo":{  
    }
}
""")

a_id = resp.get('response', [])[0].get("additionalInfo").get("successes")[0].get("aid", "")
print(a_id)