在python中获取嵌套json字符串的键

时间:2015-11-28 18:05:04

标签: python json key

{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
    {
        "batter":
            [
                { "id": "1001", "type": "Regular" },
                { "id": "1002", "type": "Chocolate" },
                { "id": "1003", "type": "Blueberry" },
                { "id": "1004", "type": "Devil's Food" }
            ]
    },
"topping":
    [
        { "id": "5001", "type": "None" },
        { "id": "5002", "type": "Glazed" },
        { "id": "5005", "type": "Sugar" },
        { "id": "5007", "type": "Powdered Sugar" },
        { "id": "5006", "type": "Chocolate with Sprinkles" },
        { "id": "5003", "type": "Chocolate" },
        { "id": "5004", "type": "Maple" }
    ]

}

以上json字符串的Concider,我想从这个json字符串中获取所有键,并使用适当的层次结构,如

id, 类型, 名称, PPU, 面糊,

然后在击球员: 面糊, 摘心

in batter:id,type

in topping:id,type

我正在使用python,有没有办法实现这个目的?

1 个答案:

答案 0 :(得分:-1)

如果这是一个字符串,请尝试:

import json
json.loads(your_variable)

如果这是一个dict,请将其解析为字符串并使用上面的方法。