简体   繁体   中英

Python - get list of keys in nested dictionary

I have a JSON file that looks like this:

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

How can I get the list of keys ("name","locality", etc.) from Objects?

EDIT: Sorry, the keys are all uniform - I just cut some of them out to keep the code/post short.

 data["objects"][0].keys()
 ['cuisines', 'postal_code', 'lat', 'id', 'categories', 'name', 'locality', 'country', 'street_address', 'has_menu', 'website_url', 'resource_uri']
with open('jsondatafile') as fp:
    data = json.load(fp)
    print list(data['objects'][0])

should do the trick...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM