如何提取嵌套的JSON数据

如何提取嵌套的JSON数据

本文介绍了如何提取嵌套的JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据JSON获取值.我已经成功遍历了JSON数据,几乎满足了我的需要!

I am trying to get a value from a data JSON. I have successfully traversed deep into the JSON data and almost have what I need!

在Python中运行以下命令:autoscaling_name = response['Reservations'][0]['Instances'][0]['Tags']

Running this command in Python :autoscaling_name = response['Reservations'][0]['Instances'][0]['Tags']

给我这个:

'Tags': [{'Key': 'Name', 'Value': 'Trove-Dev-Inst : App WebServer'}, {'Key': 'aws:autoscaling:groupName', 'Value': 'CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT'}, {'Key': 'CodeDeployProvisioningDeploymentId', 'Value': 'd-4WTRTRTRT'}, {'Key': 'Environment', 'Value': 'ernie-dev'}]

我只想获取值"CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT".这是来自键"aws:autoscaling:groupName".

I only want to get the value "CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT". This is from the key "aws:autoscaling:groupName".

如何进一步执行我的命令,使其仅返回值"CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT"?

How can I further my command to only return the value "CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT"?

推荐答案

这是完整的输出吗?这本字典包含带有嵌套字典的列表,因此您应该这样处理.假设它被称为:

Is this the full output? This a dictionary containing a list with nested dictionaries, so you should treat it that way. Suppose it is called:

A = {
    "Tags": [
        {
            "Key": "Name",
            "Value": "Trove-Dev-Inst : App WebServer"
        },
        {
            "Key": "aws:autoscaling:groupName",
            "Value": "CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT"
        },
        {
            "Key": "CodeDeployProvisioningDeploymentId",
            "Value": "d-4WTRTRTRT"
        },
        {
            "Key": "Environment",
            "Value": "ernie-dev"
        }
    ]
}

您首先要处理该对象,然后是其在字典中的键,列表中的索引以及该字典的键:

Your first adress the object, then its key in the dictionary, the index within the list and the key for that dictionary:

print(A['Tags'][1]['Value'])

输出:

CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT

根据所获得的内容,您应该尝试:

Based on what you are getting then you should try:

autoscaling_name = response['Reservations'][0]['Instances'][0]['Tags'][1]['Value']

这篇关于如何提取嵌套的JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:09