简体   繁体   English

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

[英]Return the value and data inside a JSON Array via Python

I have a JSON file named "Dev-Env-.NET-.net-details.json" with the following details below,我有一个名为"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"
    }

My goal is to return all the names and addressPrefixes under the su.net array in a much readable format.我的目标是以可读性强的格式返回addressPrefixes数组下的所有names和地址su.net I can already get the name but I have no idea and having a hard time getting the addressPrefixes since it's under properties我已经知道name了,但我不知道并且很难获得addressPrefixes ,因为它在properties

Here's my code,这是我的代码,

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

And here's the result,这是结果,

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'}]}

Here's what I'm expecting or trying to achieve,这是我期望或试图实现的目标,

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

How am I going to achieve this?我将如何实现这一目标? Thanks!谢谢!

In your code:在你的代码中:

# 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM