在python中获取json/字典中的所有键路径组合
我希望能够在JSON文件中获得指向密钥的所有不同路径。我经常获得大型JSON,但我不确定各种数据元素可能在哪里。或者我需要查询数据的各个元素。可视化JSON的树可能是不寻常的 基本上,我想得到一个所有不同路径的列表,以使未来的各种任务更容易 例如:在python中获取json/字典中的所有键路径组合,python,json,dictionary,key,Python,Json,Dictionary,Key,我希望能够在JSON文件中获得指向密钥的所有不同路径。我经常获得大型JSON,但我不确定各种数据元素可能在哪里。或者我需要查询数据的各个元素。可视化JSON的树可能是不寻常的 基本上,我想得到一个所有不同路径的列表,以使未来的各种任务更容易 例如: myjson = {'transportation':'car', 'address': {'driveway':'yes','home_address':{'state':'TX', 'city':'Houston'}}, 'work_addre
myjson = {'transportation':'car',
'address': {'driveway':'yes','home_address':{'state':'TX',
'city':'Houston'}},
'work_address':{
'state':'TX',
'city':'Sugarland',
'location':'office-tower',
'salary':30000}}
如果我能运行某种类型的循环,以下面的这种格式或其他格式获取列表,那就太好了
myjson['address']['driveway']
myjson.address
myjson.address.driveway
myjson.address.home\u地址
myjson.address.home\u address.city
myjson.address.home\u address.state
myjson.transportation
myjson.work\u地址
myjson.work\u address.city
myjson.work\u address.location
myjson.work\u address.salary
myjson.work\u address.state
例如,我从
mylist = []
for key, value in myjson.items():
mylist.append(key)
if type(value) is dict:
for key2, value2 in myjson[key].items():
mylist.append(key+'.'+key2)
print(mylist)
我想这是可行的,但我不知道如何使它无限期地迭代。例如,我将如何将其设置为3-10+层深?我认为这应该满足您的要求:
myjson = {
'transportation': 'car',
'address': {
'driveway': 'yes',
'home_address': {
'state': 'TX',
'city': 'Houston'}
},
'work_address': {
'state': 'TX',
'city': 'Sugarland',
'location': 'office-tower',
'salary': 30000}
}
def get_keys(some_dictionary, parent=None):
for key, value in some_dictionary.items():
if '{}.{}'.format(parent, key) not in my_list:
my_list.append('{}.{}'.format(parent, key))
if isinstance(value, dict):
get_keys(value, parent='{}.{}'.format(parent, key))
else:
pass
my_list = []
get_keys(myjson, parent='myjson')
print(my_list)
产出:
['myjson.transportation',
'myjson.work_address',
'myjson.work_address.city',
'myjson.work_address.state',
'myjson.work_address.location',
'myjson.work_address.salary',
'myjson.address',
'myjson.address.driveway',
'myjson.address.home_address',
'myjson.address.home_address.city',
'myjson.address.home_address.state']
关键是要在函数中递归地调用get_键 很棒的片段
以下是管理列表的版本:
def get_keys(some_dictionary, parent=None):
if isinstance(some_dictionary, str):
return
for key, value in some_dictionary.items():
if '{}.{}'.format(parent, key) not in my_list:
my_list.append('{}.{}'.format(parent, key))
if isinstance(value, dict):
get_keys(value, parent='{}.{}'.format(parent, key))
if isinstance(value, list):
for v in value:
get_keys(v, parent='{}.{}'.format(parent, key))
else:
pass
一个处理json中列表路径的实现
import json
def get_json_key_path(jsonStr, enable_index):
json_keys = []
jsonObj = json.loads(jsonStr)
def get_key_path(jsonObj, parent=None):
if not isinstance(json_obj, dict):
return
for key, value in jsonObj.items():
if not isinstance(value, list) and '{}.{}'.format(parent, key) not in json_keys:
json_keys.append('{}.{}'.format(parent, key))
if isinstance(value, dict):
get_key_path(value, parent='{}.{}'.format(parent, key))
elif isinstance(value, list):
i = 0
for obj in value:
if enable_index:
get_key_path(obj, parent='{}.{}.{}'.format(parent, key, i))
else:
get_key_path(obj, parent='{}.{}'.format(parent, key))
i = i + 1
else:
pass
get_key_path(jsonObj, "")
return [ s[1:] for s in json_keys]
这看起来像是一个树遍历问题,所以归纳法?你必须使用递归。我会试着为你写一篇帖子: