![]() |
鬼畜的钢笔 · 内置函数 — Python 3.13.5 文档· 5 天前 · |
![]() |
朝气蓬勃的油条 · Python教程:Pandas删除数据的4种情况· 4 天前 · |
![]() |
不敢表白的企鹅 · 华为开发者问答 | 华为开发者联盟· 4 天前 · |
![]() |
豪情万千的双杠 · Python ...· 2 天前 · |
![]() |
爱看书的火锅 · adb连接指定蓝牙耳机 - CSDN文库· 1 年前 · |
![]() |
爱看书的大脸猫 · C++ 操作DLL的函数 ...· 2 年前 · |
![]() |
阳刚的大白菜 · 禁止显示状态 警告 C6031 ...· 2 年前 · |
我希望能够在JSON文件中获得密钥的所有不同路径。我经常获得大的JSON,并且我不能确切地确定各种数据元素可能在哪里。或者我需要查询数据的各种元素。可视化JSON的树可能很不方便。
基本上,我希望获得所有不同路径的列表,以使未来的各种任务变得更容易。
例如:
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‘
myjson.address myjson.address.driveway myjson.address.home_address myjson.address.home_address.city myjson.address.home_address.state myjson.transportation myjson.work_address myjson.work_address.city myjson.work_address.location myjson.work_address.salary myjson.work_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+层深?
发布于 2019-09-30 00:35:18
很棒的片段!
这是一个管理列表的版本:
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
发布于 2019-03-29 06:38:11
我认为这应该能满足您的要求:
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:
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_keys()
!
发布于 2021-01-12 09:52:32
一种在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))
![]() |
鬼畜的钢笔 · 内置函数 — Python 3.13.5 文档 5 天前 |
![]() |
朝气蓬勃的油条 · Python教程:Pandas删除数据的4种情况 4 天前 |
![]() |
不敢表白的企鹅 · 华为开发者问答 | 华为开发者联盟 4 天前 |
![]() |
爱看书的火锅 · adb连接指定蓝牙耳机 - CSDN文库 1 年前 |