Python - 获取嵌套字典中的键列表

时间:2014-07-15 00:42:52

标签: python json

我有一个如下所示的JSON文件:

{
"meta": {
    "limit": 25,
    "cache-expiry": 3600
},
"objects": [
    {
        "name": "Elements Automotive",
        "locality": "Dallas",
        "street_address": "8700 Sovereign Row",
        "cuisines": [],
        "postal_code": "75247",
        "categories": [
            "other"
        ],
        "has_menu": false,
        "country": "United States",
        "lat": 32.8191949123164,
        "id": "000e090545789efeca0c",
        "website_url": "http://elementsautomotive.com/",
        "resource_uri": "/v1_0/venue/000e090545789efeca0c/"
    }
]
}

如何从对象中获取密钥列表("名称"," locality"等)?

编辑:对不起,密钥都是统一的 - 我只是把它们中的一些剪掉了以保持代码/帖子的简短。

2 个答案:

答案 0 :(得分:3)

 data["objects"][0].keys()
 ['cuisines', 'postal_code', 'lat', 'id', 'categories', 'name', 'locality', 'country', 'street_address', 'has_menu', 'website_url', 'resource_uri']

答案 1 :(得分:0)

with open('jsondatafile') as fp:
    data = json.load(fp)
    print list(data['objects'][0])

应该做的伎俩...