[英]Return the value and data inside a JSON Array via Python
我有一個名為"Dev-Env-.NET-.net-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"
}
我的目標是以可讀性強的格式返回addressPrefixes
數組下的所有names
和地址su.net
。 我已經知道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"]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.