【发布时间】:2020-05-21 17:21:39
【问题描述】:
假设我有这个 json:
json_data = json.loads("""
{
"result":{
"statistics": [
{
"name": "A1",
"year": 1994,
"award": "Y1"
},
{
"name": "X1",
"year": 1990,
"award": "Y55"
}
],
"isTrue": "yes"
}
}""")
有没有办法返回请求中找到的所有值的键路径?
请注意,json 可以更改并具有其他结构,但路径将始终特定于 json。因此,例如,这样的事情不会起作用,因为密钥每次都不相同,硬编码不是一种选择:
json_root = json_data['result]['statistics']
json_result = [something['name'] for something in json_root]
预期结果
例如:
Path = 'result/statistics/name' The result could be a dict like this : {'name':['A1', 'A2']} Path2 = 'result/isTrue' The result : {'isTrue: ['Yes']}
【问题讨论】: