【问题标题】:Unable to pull data from json using python无法使用python从json中提取数据
【发布时间】:2013-08-13 17:07:03
【问题描述】:

我有以下 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 超出范围。

我们将不胜感激。

【问题讨论】:

  • "context" 是列表,而不是字典。
  • 结构中的任何地方都没有health-status 键,只有health-state,在深度嵌套的对象中。
  • xcoder,谢谢,不知道我怎么没发现 :D Martijn Pieters,这是我的失误类型。谢谢

标签: python json


【解决方案1】:

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

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

要从该结构中取出所有 "name": "health-state" 字典,您需要进行更多处理:

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

会在 first 上下文中为您提供health-state 的匹配值列表。

演示:

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

【讨论】:

    【解决方案2】:

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

    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'
    

    【讨论】:

      【解决方案3】:

      json['reponse']['context']list,而不是 dict。结构与您想象的不完全一样。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-24
        • 2017-04-16
        • 2020-05-06
        • 2017-07-30
        • 1970-01-01
        • 1970-01-01
        • 2020-06-02
        • 2021-03-27
        相关资源
        最近更新 更多