Getting keys of nested json string in python

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

}

Concider above json string, I want to get all keys from this json string with proper hierarchy like

id , type, name, ppu, batter,

then in batters : batter, topping

in batter: id, type

in topping: id, type

I am using python, is there any way out to do the purpose?


If this is a string just try:

import json
json.loads(your_variable)

If this is a dict parse it into string and use method from above.

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

上一篇: 写入文件

下一篇: 在python中获取嵌套json字符串的键