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

{
"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" }
    ]

}

Concord上面的json字符串,我想从这个json字符串中获取所有的键,并具有适当的层次结构

ID,类型,名称,ppu,面糊,

然后在打者:面糊,打顶

在面糊:id,类型

在顶部:id,类型

我正在使用python,有什么方法可以达到目的吗?


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

import json
json.loads(your_variable)

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

链接地址: http://www.djcxy.com/p/47147.html

上一篇: Getting keys of nested json string in python

下一篇: escaping forward slashes in json output