【问题标题】:Return the value and data inside a JSON Array via Python通过 Python 返回 JSON 数组中的值和数据
【发布时间】:2021-11-27 15:05:35
【问题描述】:

我有一个名为 "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

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

【问题讨论】:

  • 而您面临的问题是...?
  • 我已经更新了问题,对此感到抱歉..

标签: python arrays json azure


【解决方案1】:

在您的代码中:

# 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"]))

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-10-09
  • 2014-09-18
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 2014-08-12
相关资源
最近更新 更多