0

我有以下 json

{
    "response": {
        "message": null,
        "exception": null,
        "context": [
            {
                "headers": null,
                "name": "aname",
                "children": [
                    {
                        "type": "cluster-connectivity",
                        "name": "cluster-connectivity"
                    },
                    {
                        "type": "consistency-groups",
                        "name": "consistency-groups"
                    },
                    {
                        "type": "devices",
                        "name": "devices"
                    },
                    {
                        "type": "exports",
                        "name": "exports"
                    },
                    {
                        "type": "storage-elements",
                        "name": "storage-elements"
                    },
                    {
                        "type": "system-volumes",
                        "name": "system-volumes"
                    },
                    {
                        "type": "uninterruptible-power-supplies",
                        "name": "uninterruptible-power-supplies"
                    },
                    {
                        "type": "virtual-volumes",
                        "name": "virtual-volumes"
                    }
                ],
                "parent": "/clusters",
                "attributes": [
                    {
                        "value": "true",
                        "name": "allow-auto-join"
                    },
                    {
                        "value": "0",
                        "name": "auto-expel-count"
                    },
                    {
                        "value": "0",
                        "name": "auto-expel-period"
                    },
                    {
                        "value": "0",
                        "name": "auto-join-delay"
                    },
                    {
                        "value": "1",
                        "name": "cluster-id"
                    },
                    {
                        "value": "true",
                        "name": "connected"
                    },
                    {
                        "value": "synchronous",
                        "name": "default-cache-mode"
                    },
                    {
                        "value": "true",
                        "name": "default-caw-template"
                    },
                    {
                        "value": "blah",
                        "name": "default-director"
                    },
                    {
                        "value": [
                            "blah",
                            "blah"
                        ],
                        "name": "director-names"
                    },
                    {
                        "value": [

                        ],
                        "name": "health-indications"
                    },
                    {
                        "value": "ok",
                        "name": "health-state"
                    },
                    {
                        "value": "1",
                        "name": "island-id"
                    },
                    {
                        "value": "blah",
                        "name": "name"
                    },
                    {
                        "value": "ok",
                        "name": "operational-status"
                    },
                    {
                        "value": [

                        ],
                        "name": "transition-indications"
                    },
                    {
                        "value": [

                        ],
                        "name": "transition-progress"
                    }
                ],
                "type": "cluster"
            }
        ],
        "custom-data": null
    }
}

我试图在 python 中使用 json 模块进行解析。我只对从中获取以下信息感兴趣。

名称 值 operation-status 值 health-state 值

这是我尝试过的。在下面的脚本数据中是从网页返回的 json

json = json.loads(data)
healthstate= json['response']['context']['operational-status']
operationalstatus = json['response']['context']['health-status']

不幸的是,我认为我必须遗漏一些东西,因为上述结果导致索引必须是整数而不是字符串的错误。

如果我尝试

healthstate= json['response'][0]

它错误地说索引 0 超出范围。

任何帮助将不胜感激。

4

3 回答 3

3

json['response']['context']是一个列表,因此对象要求您使用整数索引。

该列表中的每个项目本身又是一本字典。在这种情况下,只有一个这样的项目。

要使所有"name": "health-state"字典脱离该结构,您需要进行更多处理:

[attr['value'] for attr in json['response']['context'][0]['attributes'] if attr['name'] ==  'health-state']

health-state在第一个上下文中为您提供匹配值的列表。

演示:

>>> [attr['value'] for attr in json['response']['context'][0]['attributes'] if attr['name'] ==  'health-state']
[u'ok']
于 2013-08-13T17:13:33.280 回答
1

json['reponse']['context']是一个list,不是一个dict。结构并不完全是你想象的那样。

例如,我在那里看到的唯一“运行状态”可以用以下内容读取:

json['response']['context'][0]['attributes'][0]['operational-status']
于 2013-08-13T17:25:57.060 回答
1

您必须遵循数据结构。最好以交互方式操作数据并检查每个项目是什么。如果它是一个列表,则必须按位置对其进行索引或遍历它并检查值。如果它是一个字典,你将不得不通过它的键来索引它。例如,这里有一个函数,它获取上下文,然后遍历它的属性检查特定名称。

def get_attribute(data, attribute):
    for attrib in data['response']['context'][0]['attributes']:
        if attrib['name'] == attribute:
            return attrib['value']
    return 'Not Found'

>>> data = json.loads(s)
>>> get_attribute(data, 'operational-status')
u'ok'
>>> get_attribute(data, 'health-state')
u'ok'
于 2013-08-13T17:18:24.770 回答