我正在尝试从数据JSON获取值。我已经成功遍历了JSON数据,几乎满足了我的需求!
在Python中运行以下命令: autoscaling_name = response['Reservations'][0]['Instances'][0]['Tags']
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"。
"CodeDeploy_Ernie-dev-Autoscaling- Deploy_d-4WTRTRTRT"
"aws:autoscaling:groupName"
如何进一步执行命令以仅返回值"CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT"?
"CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT"
这是完整的输出吗?这本字典包含带有嵌套字典的列表,因此您应该这样处理。假设它被称为:
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" } ] }
您的第一个地址是对象,然后是字典中的键,列表中的索引以及该字典的键:
print(A['Tags'][1]['Value'])
输出:
CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT
编辑:根据您所得到的然后您应该尝试:
autoscaling_name = response['Reservations'][0]['Instances'][0]['Tags'][1]['Value']