如何解析json体,就像列出的字典一样

时间:2014-07-15 15:55:49

标签: json

我希望每次都能访问“uuid”,这在此代码中有两次,并且还可以访问“cloud_uuid”。身体就像下面这样:

{
"computes": [{
    "uuid": "110c607a-231c-4724-be7f-db5ed388158",
    "name": "9.4.98.33",
    "description": null,
    "version": "1.0",
    "type": "compute",
    "number_of_vms": 0,
    "status": "ACTIVE",
    "provisioning_status": {
        "status": "COMPLETED",
        "started_at": "",
        "updated_at": "",
        "status_data": null
    },
    "health_status": {
        "status": "OK",
        "alerts": [],
        "updated_at": "2014-07-11T17:09:12.194000"
    },
    "compliance_status": {
        "compliance_reasons": null,
        "is_compliant": true,
        "updated_at": null
    },
    "run_priority_order": null,
    "created": "2014-07-11T16:01:32.837821",
    "updated": "2014-07-11T17:08:16.031838",
    "capability_categories": {
        "v": [{
            "name_key": "",
            "description_key": "",
            "version": "0",
            "hidden": t,
            "priority": 1,
            "name_nls": "",
            "description_nls": ""
        }],
        "monitoring": [{
            "name_key": "m",
            "description_key": null,
            "version": "1.0",
            "hidden": true,
            "priority": 100,
            "name_nls": "monitoring",
            "description_nls": null
        }],
        "scheduler": [{
            "name_key": "",
            "description_key": null,
            "version": "1.0",
            "hidden": false,
            "priority": 20,
            "name_nls": "",
            "description_nls": null
        }],
        "network": [{
            "name_key": "",
            "description_key": "",
            "version": "1.0",
            "hidden": false,
            "priority": 10,
            "name_nls": "",
            "description_nls": ""
        }]
    },
    "links": [{
        "href": "",
        "rel": "self"
    }, {
        "href": "",
        "rel": "bookmark"
    }],
    "cloud_uuid": "b603e16e-38a6-435e-9359-79c27fee93a",
    "operating_system_uuid": "70f605e7-6512-49b4-833c-b25d47823a4"
}, {
    "uuid": "7383f4a5-dc0a-420b-806c-abbd49c1655a",
    "name": "9.4.193.20",
    "description": null,
    "version": "1.0",
    "type": "compute"

您可以在下面提供帮助吗,我按照评论中的回答尝试了代码: 假设身体是“云”而不是“计算” 我尝试在cloud_uuid = ((e['cloud_uuid']中为e获取类似于dict['clouds'] if e['name'] == name_to_find), None)的cloud_uuid 它抛出错误 - >

  

cloud_uuid =((e ['cloud_uuid']表示e在dict ['clouds']如果e ['name'] ==   name_to_find),无)TypeError:'type'对象是unsubscriptable

1 个答案:

答案 0 :(得分:0)

在python中它非常简单:

[(e['uuid'], e['cloud_uuid']) for e in dict['computes']]

编辑: 看起来我忽略了一对方括号。尝试:

[(_['computes'][0]['uuid'], _['cloud_uuid']) for _ in data]