gpt4 book ai didi

python - 通过 Python 返回 JSON 数组中的值和数据

转载 作者:行者123 更新时间:2023-12-03 05:25:13 25 4
gpt4 key购买 nike

我有一个名为 “Dev-Env-VNET-vnet-details.json” 的 JSON 文件,其中包含以下详细信息,

  {
"location": "southeastasia",
"name": "Dev-Env-VNET",
"properties": {
"addressSpace": {
"addressPrefixes": [
"10.0.0.0/16",
"172.16.0.0/16",
"192.168.1.0/24"
]
},
"enableDdosProtection": false,
"provisioningState": "Succeeded",
"subnets": [
{
"name": "default",
"properties": {
"addressPrefix": "10.0.0.0/24",
"delegations": [],
"privateEndpointNetworkPolicies": "Enabled",
"privateLinkServiceNetworkPolicies": "Enabled",
"provisioningState": "Succeeded"
},
"resourceGroup": "Dev-Env",
"type": "Microsoft.Network/virtualNetworks/subnets"
},
{
"name": "Dev-LAN",
"properties": {
"addressPrefix": "172.16.0.0/24",
"delegations": [],
"privateEndpointNetworkPolicies": "Enabled",
"privateLinkServiceNetworkPolicies": "Enabled",
"provisioningState": "Succeeded",
"serviceEndpoints": [
{
"locations": [
"*"
],
"provisioningState": "Succeeded",
"service": "Microsoft.AzureCosmosDB"
},
{
"locations": [
"southeastasia"
],
"provisioningState": "Succeeded",
"service": "Microsoft.Sql"
},
{
"locations": [
"southeastasia",
"eastasia"
],
"provisioningState": "Succeeded",
"service": "Microsoft.Storage"
},
{
"locations": [
"*"
],
"provisioningState": "Succeeded",
"service": "Microsoft.KeyVault"
}
]
},
"resourceGroup": "Dev-Env",
"type": "Microsoft.Network/virtualNetworks/subnets"
}
]
},
"resourceGroup": "Dev-Env",
"tags": {
"Environment": "Dev"
},
"type": "Microsoft.Network/virtualNetworks"
}

我的目标是以易于阅读的格式返回subnet数组下的所有namesaddressPrefixes。我已经可以获得name,但我不知道并且很难获得addressPrefixes,因为它位于properties

这是我的代码,

import json

vnet_data = open('.\Dev-Env-VNET-vnet-details.json')
vnet_json = json.load(vnet_data)

get_subnets = vnet_json['properties']
subnet = get_subnets['subnets']
for s in range(len(subnet)):
print("Subnet name: {}, addressPrefix: {} ".format(subnet[s]["name"],subnet[s]["properties"]))

这是结果,

Subnet name: default, addressPrefix: {'addressPrefix': '10.0.0.0/24', 'delegations': [], 'privateEndpointNetworkPolicies': 'Enabled', 'privateLinkServiceNetworkPolicies': 'Enabled', 'provisioningState': 'Succeeded'} 
Subnet name: Dev-LAN, addressPrefix: {'addressPrefix': '172.16.0.0/24', 'delegations': [], 'privateEndpointNetworkPolicies': 'Enabled', 'privateLinkServiceNetworkPolicies': 'Enabled', 'provisioningState': 'Succeeded', 'serviceEndpoints': [{'locations': ['*'], 'provisioningState': 'Succeeded', 'service': 'Microsoft.AzureCosmosDB'}, {'locations': ['southeastasia'], 'provisioningState': 'Succeeded', 'service': 'Microsoft.Sql'}, {'locations': ['southeastasia', 'eastasia'], 'provisioningState': 'Succeeded', 'service': 'Microsoft.Storage'}, {'locations': ['*'], 'provisioningState': 'Succeeded', 'service': 'Microsoft.KeyVault'}]}

这是我期望或试图实现的目标,

Subnet name: default, addressPrefix: 10.0.0.0/24
Subnet name: Dev-LAN, addressPrefix: 172.16.0.0/24

我该如何实现这一目标?谢谢!

最佳答案

在您的代码中:

# Instead of 
#for s in range(len(subnet)):
# print("Subnet name: {}, addressPrefix: {} ".format(subnet[s]["name"],subnet[s]["properties"]))

for s in subnet:
print("Subnet name: {}, addressPrefix: {} ".format(
s["name"], s["properties"]["addressPrefix"]))

关于python - 通过 Python 返回 JSON 数组中的值和数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69482574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com