我正在尝试循环下面的json并打印或收集所有的VpcEndPointId值。
response = {
"VpcEndpoints": [
{
"VpcEndpointId": "vpce-123",
"VpcEndpointType": "GatewayLoadBalancer",
"VpcId": "vpc-test",
"ServiceName": "com.amazonaws.com",
"State": "available",
"SubnetIds": [
"subnet-random"
],
"IpAddressType": "ipv4",
"RequesterManaged": True,
"NetworkInterfaceIds": [
"eni-123"
],
"CreationTimestamp": "2022-10-28T01:23:23.924Z",
"Tags": [
{
"Key": "AWSNetworkFirewallManaged",
"Value": "true"
},
{
"Key": "Firewall",
"Value": "arn:aws:network-firewall:us-west-2"
}
],
"OwnerId": "123"
},
{
"VpcEndpointId": "vpce-123",
"VpcEndpointType": "GatewayLoadBalancer",
"VpcId": "vpc-<value>",
"ServiceName": "com.amazonaws.vpce.us-west-2",
"State": "available",
"SubnetIds": [
"subnet-<number>"
],
"IpAddressType": "ipv4",
"RequesterManaged": True,
"NetworkInterfaceIds": [
"eni-<value>"
],
"CreationTimestamp": "2022-10-28T01:23:42.113Z",
"Tags": [
{
"Key": "AWSNetworkFirewallManaged",
"Value": "True"
},
{
"Key": "Firewall",
"Value": "arn:aws:network-firewall:%l"
}
],
"OwnerId": "random"
}
]
}
我遇到的问题是字典被嵌套在一个列表中。我已经能够通过一个问题,我可以在下面的代码中打印VpcEndPointId
KEYS,但仍然试图弄清楚如何打印值。
我尝试使用。values,但它似乎类型是一个字符串,当我尝试它下面的代码
for endpoint in response['VpcEndpoints']:
#for vpc_endpoint in endpoint['VpcEndpointId']:
for vpc_endpoint in endpoint:
if vpc_endpoint == 'VpcEndpointId':
type(vpc_endpoint)
我肯定有什么我错过了,可能有一个更简单的解决方案,所以任何建议应该有所帮助,谢谢!
要打印VpcEndpointId
的值,使用一个循环就足够了:
for endpoint in response['VpcEndpoints']:
print(endpoint['VpcEndpointId'])