需要从JSON中读取一些数据

时间:2016-10-07 23:36:28

标签: python json

我需要为这个json

中的每个代理人创建一个get(id,name,fraction id)
{
    "id": "75785",
    "title": "(за основу)",
    "asozdUrl": null,
    "datetime": "2011-12-21T12:20:26+0400",
    "votes": [
        {
            "deputy": {
                "id": "99111772",
                "name": "Абалаков Александр Николаевич",
                "faction": {
                    "id": "72100004",
                    "title": "КПРФ"
                }
            },
            "result": "accept"
        },
        {
            "deputy": {
                "id": "99100491",
                "name": "Абдулатипов Рамазан Гаджимурадович",
                "faction": {
                    "id": "72100024",
                    "title": "ЕР"
                }
            },
            "result": "none"
        }
    .......,`   etc

我的代码看起来像这样:

urlData = "https://raw.githubusercontent.com/data-dumaGovRu/vote/master/poll/2011-12-21/75785.json"
response = urllib.request.urlopen(urlData)
content = response.read()
data = json.loads(content.decode("utf8"))
for i in data:    
    #print(data["name"])

` 我不知道如何处理#print行,我应该怎么写呢?

1 个答案:

答案 0 :(得分:3)

您可以使用data['votes']访问包含代表的列表。通过列表迭代,您可以像使用dict键查找一样访问您感兴趣的键。嵌套的dicts意味着你必须从根到你感兴趣的地方遍历键:

for d in data['votes']:
    print(d['deputy']['id'], d['deputy']['name'], d['deputy']['faction']['id'])