Python 在不知道键值的情况下解析JSON
我知道如何在知道键值的情况下解析JSON,但现在我想从一个不是我的JSON中获取键值,这样我就可以知道键名,例如我有这个JSONPython 在不知道键值的情况下解析JSON,python,json,Python,Json,我知道如何在知道键值的情况下解析JSON,但现在我想从一个不是我的JSON中获取键值,这样我就可以知道键名,例如我有这个JSON [ { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", "address": { "street": "Kulas Light", "suite": "Apt. 556",
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
},
...
]
因此,从现在起,我有:
with open('users.json') as f:
data = json.load(f)
如果我打印数据
,我可以看到加载的所有JSON,所以我的问题是,如何在不知道名称的情况下打印所有键和嵌套对象
我的目标是拥有像
身份证件
名称
用户名
电子邮件
包含street、suite、city、zipcode、geo(包含lat、long等)的地址。这是一个递归生成器,它将扫描嵌套列表/字典结构,就像将JSON加载到Python中一样。它显示与每个值关联的字典键和列表索引的序列 我稍微修改了您的数据,以说明它如何处理嵌套在dict中的列表
data = [
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
},
"other": ["This", "is", "a list"]
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
},
"other": ["This", "is", "another list"]
},
]
def show_indices(obj, indices):
for k, v in obj.items() if isinstance(obj, dict) else enumerate(obj):
if isinstance(v, (dict, list)):
yield from show_indices(v, indices + [k])
else:
yield indices + [k], v
for keys, v in show_indices(data, []):
print(keys, v)
输出
[0, 'id'] 1
[0, 'name'] Leanne Graham
[0, 'username'] Bret
[0, 'email'] Sincere@april.biz
[0, 'address', 'street'] Kulas Light
[0, 'address', 'suite'] Apt. 556
[0, 'address', 'city'] Gwenborough
[0, 'address', 'zipcode'] 92998-3874
[0, 'address', 'geo', 'lat'] -37.3159
[0, 'address', 'geo', 'lng'] 81.1496
[0, 'phone'] 1-770-736-8031 x56442
[0, 'website'] hildegard.org
[0, 'company', 'name'] Romaguera-Crona
[0, 'company', 'catchPhrase'] Multi-layered client-server neural-net
[0, 'company', 'bs'] harness real-time e-markets
[0, 'other', 0] This
[0, 'other', 1] is
[0, 'other', 2] a list
[1, 'id'] 2
[1, 'name'] Ervin Howell
[1, 'username'] Antonette
[1, 'email'] Shanna@melissa.tv
[1, 'address', 'street'] Victor Plains
[1, 'address', 'suite'] Suite 879
[1, 'address', 'city'] Wisokyburgh
[1, 'address', 'zipcode'] 90566-7771
[1, 'address', 'geo', 'lat'] -43.9509
[1, 'address', 'geo', 'lng'] -34.4618
[1, 'phone'] 010-692-6593 x09125
[1, 'website'] anastasia.net
[1, 'company', 'name'] Deckow-Crist
[1, 'company', 'catchPhrase'] Proactive didactic contingency
[1, 'company', 'bs'] synergize scalable supply-chains
[1, 'other', 0] This
[1, 'other', 1] is
[1, 'other', 2] another list
Proactive didactic contingency
{'name': 'Deckow-Crist', 'catchPhrase': 'some new thing', 'bs': 'synergize scalable supply-chains'}
您可以使用这些列表访问任何项目,例如
keys = [1, 'company', 'catchPhrase']
obj = data
for k in keys:
obj = obj[k]
print(obj)
输出
[0, 'id'] 1
[0, 'name'] Leanne Graham
[0, 'username'] Bret
[0, 'email'] Sincere@april.biz
[0, 'address', 'street'] Kulas Light
[0, 'address', 'suite'] Apt. 556
[0, 'address', 'city'] Gwenborough
[0, 'address', 'zipcode'] 92998-3874
[0, 'address', 'geo', 'lat'] -37.3159
[0, 'address', 'geo', 'lng'] 81.1496
[0, 'phone'] 1-770-736-8031 x56442
[0, 'website'] hildegard.org
[0, 'company', 'name'] Romaguera-Crona
[0, 'company', 'catchPhrase'] Multi-layered client-server neural-net
[0, 'company', 'bs'] harness real-time e-markets
[0, 'other', 0] This
[0, 'other', 1] is
[0, 'other', 2] a list
[1, 'id'] 2
[1, 'name'] Ervin Howell
[1, 'username'] Antonette
[1, 'email'] Shanna@melissa.tv
[1, 'address', 'street'] Victor Plains
[1, 'address', 'suite'] Suite 879
[1, 'address', 'city'] Wisokyburgh
[1, 'address', 'zipcode'] 90566-7771
[1, 'address', 'geo', 'lat'] -43.9509
[1, 'address', 'geo', 'lng'] -34.4618
[1, 'phone'] 010-692-6593 x09125
[1, 'website'] anastasia.net
[1, 'company', 'name'] Deckow-Crist
[1, 'company', 'catchPhrase'] Proactive didactic contingency
[1, 'company', 'bs'] synergize scalable supply-chains
[1, 'other', 0] This
[1, 'other', 1] is
[1, 'other', 2] another list
Proactive didactic contingency
{'name': 'Deckow-Crist', 'catchPhrase': 'some new thing', 'bs': 'synergize scalable supply-chains'}
或者,如果要修改项目:
keys = [1, 'company', 'catchPhrase']
obj = data
for k in keys[:-1]:
obj = obj[k]
obj[keys[-1]] = "some new thing"
print(data[1]['company'])
输出
[0, 'id'] 1
[0, 'name'] Leanne Graham
[0, 'username'] Bret
[0, 'email'] Sincere@april.biz
[0, 'address', 'street'] Kulas Light
[0, 'address', 'suite'] Apt. 556
[0, 'address', 'city'] Gwenborough
[0, 'address', 'zipcode'] 92998-3874
[0, 'address', 'geo', 'lat'] -37.3159
[0, 'address', 'geo', 'lng'] 81.1496
[0, 'phone'] 1-770-736-8031 x56442
[0, 'website'] hildegard.org
[0, 'company', 'name'] Romaguera-Crona
[0, 'company', 'catchPhrase'] Multi-layered client-server neural-net
[0, 'company', 'bs'] harness real-time e-markets
[0, 'other', 0] This
[0, 'other', 1] is
[0, 'other', 2] a list
[1, 'id'] 2
[1, 'name'] Ervin Howell
[1, 'username'] Antonette
[1, 'email'] Shanna@melissa.tv
[1, 'address', 'street'] Victor Plains
[1, 'address', 'suite'] Suite 879
[1, 'address', 'city'] Wisokyburgh
[1, 'address', 'zipcode'] 90566-7771
[1, 'address', 'geo', 'lat'] -43.9509
[1, 'address', 'geo', 'lng'] -34.4618
[1, 'phone'] 010-692-6593 x09125
[1, 'website'] anastasia.net
[1, 'company', 'name'] Deckow-Crist
[1, 'company', 'catchPhrase'] Proactive didactic contingency
[1, 'company', 'bs'] synergize scalable supply-chains
[1, 'other', 0] This
[1, 'other', 1] is
[1, 'other', 2] another list
Proactive didactic contingency
{'name': 'Deckow-Crist', 'catchPhrase': 'some new thing', 'bs': 'synergize scalable supply-chains'}
试试这样的
for d in data:
for key in d.keys():
print(d[key])
最终,我认为这没有什么用处。在某个时候,您必须亲自检查JSON以了解您想要什么。如果获得所有键并删除值,则仍然需要维护嵌套结构,以便知道如何实际获取它们。。。这意味着无论如何都要阅读它。你是什么意思?嗯,好的,那么你只是想知道如何迭代数据?你所拥有的只是一个包含嵌套字典的列表(只要你这样做,
data=json.load(f)
它就不再是json了)。是的,我知道,我正在尝试从该列表中获取密钥。一旦你像你那样加载它,它只是一个常规的dict