无法使用python从json中提取数据

时间:2013-08-13 17:07:04

标签: python json

我有以下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模块进行解析。我只对收到以下信息感兴趣。

名称值 运营状况价值 健康状况值

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

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

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

如果我尝试

healthstate= json['response'][0]

错误说索引0超出范围。

感谢任何帮助。

3 个答案:

答案 0 :(得分: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']

答案 1 :(得分: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'

答案 2 :(得分:1)

json['reponse']['context']list,而不是dict。结构并不完全符合您的想象。

例如,我在其中看到的唯一“操作状态”可以通过以下方式阅读:

json['response']['context'][0]['attributes'][0]['operational-status']