从Json收集一个值

时间:2014-11-24 17:28:54

标签: python json

我从脚本中收集了这个Json数据,并想知道如何解析数据以获取id属性。甚至不确定要查找什么。

{
    "status": "success",
    "data": [
        {
            "notes": [],
            "handles": {
                "twitter": [
                    {
                        "networkinstance": "twitter",
                        "Uid": "fefewf32ff32f232f3f",
                        "id": "243425324242",
                        "@class": "Provider"
                    }
                ]
            },
            "id": "g43g5434g43f2f3",
            "tags": []
        }
    ]
}

1 个答案:

答案 0 :(得分:4)

使用json module解码后,这只是一个包含列表和字符串以及其他词典的字典:

import json

json_result = json.loads(json_string)
id = json_result['data'][0]['id']

演示:

>>> import json
>>> sample = '''\
... {
...     "status": "success",
...     "data": [
...         {
...             "notes": [],
...             "handles": {
...                 "twitter": [
...                     {
...                         "networkinstance": "twitter",
...                         "Uid": "fefewf32ff32f232f3f",
...                         "id": "243425324242",
...                         "@class": "Provider"
...                     }
...                 ]
...             },
...             "id": "g43g5434g43f2f3",
...             "tags": []
...         }
...     ]
... }
... '''
>>> json_result = json.loads(sample)
>>> json_result['data'][0]['id']
u'g43g5434g43f2f3'