简体   繁体   English

如何从给定的json获取特定值

[英]How to get a particular value from the given json

I have the following json 我有以下JSON

`{
    "count": 4,
    "node": [{
        "assetRecord": {
            "additionalhardware": null,
            "admin": null,
            "assetNumber": null,
            "autoenable": null,
            "building": null,
            "category": "Unspecified",
            "circuitId": null,
            "comment": null,
            "connection": null,
            "cpu": null,
            "dateInstalled": null,
            "department": null,
            "description": null,
            "displayCategory": null,
            "division": null,
            "enable": null,
            "floor": null,
            "hdd1": null,
            "hdd2": null,
            "hdd3": null,
            "hdd4": null,
            "hdd5": null,
            "hdd6": null,
            "id": 10817,
            "inputpower": null,
            "lastModifiedBy": "",
            "lastModifiedDate": 1491855810758,
            "lease": null,
            "leaseExpires": null,
            "maintContractExpiration": null,
            "maintContractNumber": null,
            "maintcontract": null,
            "managedObjectInstance": null,
            "managedObjectType": null,
            "manufacturer": null,
            "modelNumber": null,
            "notifyCategory": null,
            "numpowersupplies": null,
            "operatingSystem": null,
            "password": null,
            "pollerCategory": null,
            "port": null,
            "rack": null,
            "rackunitheight": null,
            "ram": null,
            "region": null,
            "room": null,
            "serialNumber": null,
            "slot": null,
            "snmpcommunity": null,
            "storagectrl": null,
            "supportPhone": null,
            "thresholdCategory": null,
            "username": null,
            "vendor": null,
            "vendorAssetNumber": null,
            "vendorFax": null,
            "vendorPhone": null,
            "vmwareManagedEntityType": null,
            "vmwareManagedObjectId": null,
            "vmwareManagementServer": null,
            "vmwareState": null,
            "vmwareTopologyInfo": null
        },
        "categories": [],
        "createTime": 1491855810758,
        "foreignId": null,
        "foreignSource": null,
        "id": "564",
        "label": "10.21.204.174",
        "labelSource": "A",
        "lastCapsdPoll": 1491855810984,
        "type": "A"
    }, {
        "assetRecord": {
            "additionalhardware": null,
            "admin": null,
            "assetNumber": null,
            "autoenable": null,
            "building": null,
            "category": "Unspecified",
            "circuitId": null,
            "comment": null,
            "connection": null,
            "cpu": null,
            "dateInstalled": null,
            "department": null,
            "description": null,
            "displayCategory": null,
            "division": null,
            "enable": null,
            "floor": null,
            "hdd1": null,
            "hdd2": null,
            "hdd3": null,
            "hdd4": null,
            "hdd5": null,
            "hdd6": null,
            "id": 10589,
            "inputpower": null,
            "lastModifiedBy": "",
            "lastModifiedDate": 1490819238331,
            "lease": null,
            "leaseExpires": null,
            "maintContractExpiration": null,
            "maintContractNumber": null,
            "maintcontract": null,
            "managedObjectInstance": null,
            "managedObjectType": null,
            "manufacturer": null,
            "modelNumber": null,
            "notifyCategory": null,
            "numpowersupplies": null,
            "operatingSystem": null,
            "password": null,
            "pollerCategory": null,
            "port": null,
            "rack": null,
            "rackunitheight": null,
            "ram": null,
            "region": null,
            "room": null,
            "serialNumber": null,
            "slot": null,
            "snmpcommunity": null,
            "storagectrl": null,
            "supportPhone": null,
            "thresholdCategory": null,
            "username": null,
            "vendor": null,
            "vendorAssetNumber": null,
            "vendorFax": null,
            "vendorPhone": null,
            "vmwareManagedEntityType": null,
            "vmwareManagedObjectId": null,
            "vmwareManagementServer": null,
            "vmwareState": null,
            "vmwareTopologyInfo": null
        },
        "categories": [],
        "createTime": 1490819238331,
        "foreignId": null,
        "foreignSource": null,
        "id": "561",
        "label": "10.21.205.148",
        "labelSource": "A",
        "lastCapsdPoll": 1490819238557,
        "type": "A"
    }, {
        "assetRecord": {
            "additionalhardware": null,
            "admin": null,
            "assetNumber": null,
            "autoenable": null,
            "building": null,
            "category": "Unspecified",
            "circuitId": null,
            "comment": null,
            "connection": null,
            "cpu": null,
            "dateInstalled": null,
            "department": null,
            "description": null,
            "displayCategory": null,
            "division": null,
            "enable": null,
            "floor": null,
            "hdd1": null,
            "hdd2": null,
            "hdd3": null,
            "hdd4": null,
            "hdd5": null,
            "hdd6": null,
            "id": 7554,
            "inputpower": null,
            "lastModifiedBy": "",
            "lastModifiedDate": 1477563144159,
            "lease": null,
            "leaseExpires": null,
            "maintContractExpiration": null,
            "maintContractNumber": null,
            "maintcontract": null,
            "managedObjectInstance": null,
            "managedObjectType": null,
            "manufacturer": null,
            "modelNumber": null,
            "notifyCategory": null,
            "numpowersupplies": null,
            "operatingSystem": null,
            "password": null,
            "pollerCategory": null,
            "port": null,
            "rack": null,
            "rackunitheight": null,
            "ram": null,
            "region": null,
            "room": null,
            "serialNumber": null,
            "slot": null,
            "snmpcommunity": null,
            "storagectrl": null,
            "supportPhone": null,
            "thresholdCategory": null,
            "username": null,
            "vendor": null,
            "vendorAssetNumber": null,
            "vendorFax": null,
            "vendorPhone": null,
            "vmwareManagedEntityType": null,
            "vmwareManagedObjectId": null,
            "vmwareManagementServer": null,
            "vmwareState": null,
            "vmwareTopologyInfo": null
        },
        "categories": [],
        "createTime": 1477563144159,
        "foreignId": null,
        "foreignSource": null,
        "id": "462",
        "label": "10.21.50.153",
        "labelSource": "A",
        "lastCapsdPoll": 1490722710498,
        "type": "A"
    }, {
        "assetRecord": {
            "additionalhardware": null,
            "admin": null,
            "assetNumber": null,
            "autoenable": null,
            "building": null,
            "category": "Unspecified",
            "circuitId": null,
            "comment": null,
            "connection": null,
            "cpu": null,
            "dateInstalled": null,
            "department": null,
            "description": null,
            "displayCategory": null,
            "division": null,
            "enable": null,
            "floor": null,
            "hdd1": null,
            "hdd2": null,
            "hdd3": null,
            "hdd4": null,
            "hdd5": null,
            "hdd6": null,
            "id": 5119,
            "inputpower": null,
            "lastModifiedBy": "",
            "lastModifiedDate": 1461419153008,
            "lease": null,
            "leaseExpires": null,
            "maintContractExpiration": null,
            "maintContractNumber": null,
            "maintcontract": null,
            "managedObjectInstance": null,
            "managedObjectType": null,
            "manufacturer": null,
            "modelNumber": null,
            "notifyCategory": null,
            "numpowersupplies": null,
            "operatingSystem": null,
            "password": null,
            "pollerCategory": null,
            "port": null,
            "rack": null,
            "rackunitheight": null,
            "ram": null,
            "region": null,
            "room": null,
            "serialNumber": null,
            "slot": null,
            "snmpcommunity": null,
            "storagectrl": null,
            "supportPhone": null,
            "thresholdCategory": null,
            "username": null,
            "vendor": null,
            "vendorAssetNumber": null,
            "vendorFax": null,
            "vendorPhone": null,
            "vmwareManagedEntityType": null,
            "vmwareManagedObjectId": null,
            "vmwareManagementServer": null,
            "vmwareState": null,
            "vmwareTopologyInfo": null
        },
        "categories": [],
        "createTime": 1461419153008,
        "foreignId": null,
        "foreignSource": null,
        "id": "389",
        "label": "dummyname_xv1",
        "labelSource": "A",
        "lastCapsdPoll": 1490722710502,
        "type": "A"
    }],
    "totalCount": 271
}

This above code is actually dumped in a file (say abc.txt) and it is a valid JSON format. 上面的代码实际上转储到文件中(例如abc.txt),并且是有效的JSON格式。 I need to open the file and traverse to the "label" field. 我需要打开文件并遍历“标签”字段。 I need to write a code which should extract/print the "label" value only if it is a IP ie something in format of 0.0.0.0 我需要编写一个代码,仅当它是IP时(即格式为0.0.0.0的东西),才应提取/打印“标签”值

How to do so ? 怎么做?

Try to use json module to parse json file and iterate node : 尝试使用json模块来解析json文件并迭代node

json.load() json.load()

Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using this conversion table. 使用此转换表将fp(支持.read()的包含JSON文档的类似文件的对象)反序列化为Python对象。

If you want to validate IP string is valid , you can use inet_aton () . 如果要验证IP字符串是否有效,则可以使用inet_aton()

Convert an IPv4 address from dotted-quad string format (for example, '123.45.67.89') to 32-bit packed binary format, as a string four characters in length. 将IPv4地址从点分四进制字符串格式(例如'123.45.67.89')转换为32位压缩二进制格式,即长度为四个字符的字符串。

import json
import socket


for i in json.load(open('ooo.json'))["node"]:
    try:
        socket.inet_aton(i["label"])
        print(i["label"])
    except Exception as e:
        pass

Result: 结果:

10.21.204.174
10.21.205.148
10.21.50.153

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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