打印python中json文件的所有键

print All the keys of a json file in python

我有一个文件夹,其中有大约20000个JSON文件。我想找出每个JSON的所有唯一键,并对所有键进行联合。但是,我只是被困在了最初的一步。我可以找到单个JSON文件的密钥。

到目前为止,我已经编写了以下代码:

1
2
3
4
5
6
7
8
from pprint import pprint
import json
json_data=open("/Users/akira/out/1.json")
jdata = json.load(json_data)

for key, value in jdata:
   pprint("Key:")
   pprint(key)

它给我一个错误如下:

1
2
3
4
Traceback (most recent call last):
 File"/Users/akira/PycharmProjects/csci572/linkedbased.py",     line 8, in <module>
   for key, value in jdata:
 ValueError: need more than 1 value to unpack

我的JSON是一个嵌套的JSON。请告诉我怎样才能拿到所有的钥匙。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{
"a":"Offer",
"inLanguage":"et",
"availabl": {
   "a":"Place",
   "address": {
       "a":"PostalAddress",
       "name":"Oklahoma"
    }
},
"description":"Smith and Wesson 686 357 magnum 6 inch barrel wood handle great condition shoots great.",
"priceCurrency":"USD",
"geonames_address": [
    {
       "a":"PopulatedPlace",
       "hasIdentifier": {
           "a":"Identifier",
           "label":"4552707",
           "hasType":"http://dig.isi.edu/gazetteer/data/SKOS/IdentifierTypes/GeonamesId"
        },
       "hasPreferredName": {
           "a":"Name",
           "label":"Tahlequah"
        },
       "uri":"http://dig.isi.edu/gazetteer/data/geonames/place/4552707",
       "fallsWithinState1stDiv": {
           "a":"State1stDiv",
           "uri":"http://dig.isi.edu/gazetteer/data/geonames/place/State1stDiv/US_OK",
           "hasName": {
               "a":"Name",
               "label":"Oklahoma"
            }
        },
       "score": 0.5,
       "fallsWithinCountry": {
           "a":"Country",
           "uri":"http://dig.isi.edu/gazetteer/data/geonames/place/Country/US",
           "hasName": {
               "a":"Name",
               "label":"United States"
            }
        },
       "fallsWithinCountyProvince2ndDiv": {
           "a":"CountyProvince2ndDiv",
           "uri":"http://dig.isi.edu/gazetteer/data/geonames/place/CountyProvince2ndDiv/US_OK_021"
        },
       "geo": {
           "lat": 35.91537,
           "lon": -94.96996
        }
    }
],
"price": 750,
"title":"For Sale: Smith &amp; Wesson 686",
"publisher": {
   "a":"Organization",
   "name":"armslist.com",
   "uri":"http://dig.isi.edu/weapons/data/organization/armslist"
},
"uri":"http://dig.isi.edu/weapons/data/page/13AD9516F01012C5F89E8AADAE5D7E1E2BA97FF9/1433463841000/processed",
"seller": {
   "a":"PersonOrOrganization",
   "description":"Private Party"
} //, ...
}


for key, value in jdata.items():代替for key, value in jdata:,如下:

1
2
3
for key, value in data.items():
    pprint("Key:")
    pprint(key)

查看dict文档:

items():

Return a new view of the dictionary’s items ((key, value) pairs).

编辑:如果要获取所有嵌套键,而不仅仅是顶级键,可以采用类似于另一个答案中建议的方法,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def get_keys(dl, keys_list):
    if isinstance(dl, dict):
        keys_list += dl.keys()
        map(lambda x: get_keys(x, keys_list), dl.values())
    elif isinstance(dl, list):
        map(lambda x: get_keys(x, keys_list), dl)

keys = []
get_keys(jdata, keys)

print(keys)
# [u'a', u'inLanguage', u'description', u'priceCurrency', u'geonames_address', u'price', u'title', u'availabl', u'uri', u'seller', u'publisher', u'a', u'hasIdentifier', u'hasPreferredName', u'uri', u'fallsWithinState1stDiv', u'score', u'fallsWithinCountry', u'fallsWithinCountyProvince2ndDiv', u'geo', u'a', u'hasType', u'label', u'a', u'label', u'a', u'uri', u'hasName', u'a', u'label', u'a', u'uri', u'hasName', u'a', u'label', u'a', u'uri', u'lat', u'lon', u'a', u'address', u'a', u'name', u'a', u'description', u'a', u'name', usury']

print(list(set(keys)))    # unique list of keys
# [u'inLanguage', u'fallsWithinState1stDiv', u'label', u'hasName', u'title', u'hasPreferredName', u'lon', u'seller', u'score', u'description', u'price', u'address', u'lat', u'fallsWithinCountyProvince2ndDiv', u'geo', u'a', u'publisher', u'hasIdentifier', u'name', u'priceCurrency', u'geonames_address', u'hasType', u'availabl', u'uri', u'fallsWithinCountry']

你应该在for key, value in jdata中使用dict.items()dict.iteritems()

所以,应该是

1
for key, value in jdata.items():

1
for key, value in jdata.iteritems():

分别用于python3和python2。

请参阅此问题的答案以了解两者的区别:dict.items()和dict.iteritems()之间的区别是什么?

如果只需要迭代字典的键,甚至可以尝试dict.keys()dict.iterkeys()