Python 浏览包含嵌套列表和字典的动态json
我有多层json文件,里面嵌套了Python 浏览包含嵌套列表和字典的动态json,python,json,python-3.x,list,dictionary,Python,Json,Python 3.x,List,Dictionary,我有多层json文件,里面嵌套了list和dictionnary,它们看起来像这样,但大小和级别不同: { "mass_update": [ { "lvl-1": "lvl-1.1" }, { "lvl-1": "lvl-1.2" }, {
list
和dictionnary
,它们看起来像这样,但大小和级别不同:
{
"mass_update": [
{
"lvl-1": "lvl-1.1"
},
{
"lvl-1": "lvl-1.2"
},
{
"lvl-1": "lvl-1.3"
},
[
{
"lvl-2": "lvl-2.1",
"lvl-2": "lvl-2.1.2"
},
[
{
"lvl-3": "lvl-3.1"
},
{
"lvl-3": "lvl-3.2"
},
[
{
"lvl-4": "lvl-4.1",
"lvl-4": "lvl-4.1.2"
},
{
"lvl-4": "lvl-4.2",
"lvl-4": "lvl-4.2.2"
}
]
],
{
"lvl-2": "lvl-2.2",
"lvl-2": "lvl-2.2.2"
},
[
{
"lvl-3": "lvl-3.3"
},
{
"lvl-3": "lvl-3.4"
},
[
{
"lvl-4": "lvl-4.3",
"lvl-4": "lvl-4.3.2"
},
{
"lvl-4": "lvl-4.4",
"lvl-4": "lvl-4.4.2"
}
]
]
]
]
}
我试图阅读json和打印
每一个键
值
组合。当我在一个嵌套的列表的末尾时,我想执行一个函数
,比如说现在有一个打印(“这是列表的末尾,耶:-)”
我尝试了一些东西,比如创建一个类
,它有一个变量
,我在这里储存我的字典
或我的列表
:
class MassUpt:
def __init__(self, my_dict):
self.my_dict = my_dict
def get_my_dict(self):
return self.my_dict
def get_obj(full_dict): #full_dict contains the list "mass_update" that is in all json files
for item in full_dict:
if isinstance(item, list):
obj_dict = MassUpt(item)
mass_upt_dict.append(obj_dict)
return get_obj(item)
else:
obj_dict = MassUpt(item)
mass_upt_dict.append(obj_dict)
print()
我将类中的obj
保存在全局列表中
:
class MassUpt:
def __init__(self, my_dict):
self.my_dict = my_dict
def get_my_dict(self):
return self.my_dict
def get_obj(full_dict): #full_dict contains the list "mass_update" that is in all json files
for item in full_dict:
if isinstance(item, list):
obj_dict = MassUpt(item)
mass_upt_dict.append(obj_dict)
return get_obj(item)
else:
obj_dict = MassUpt(item)
mass_upt_dict.append(obj_dict)
print()
然后,我执行另一个函数
,即目前,打印键值
组合:
def printing_obj_lst(full_dict):
lst_test = []
for item in full_dict:
lst_test.append(item.get_my_dict())
for item in lst_test:
if isinstance(item, list):
print("Calling next object")
else:
for k, v in item.items():
print("Key: " + str(k) + " Value: " + str(v))
以下是我执行的main
:
full_dict = mass_upt_res_data_json["mass_update"]
get_obj(full_dict)
printing_obj_lst(mass_upt_dict)
这是我的输出:
Key: lvl-1 Value: lvl-1.1
Key: lvl-1 Value: lvl-1.2
Key: lvl-1 Value: lvl-1.3
Calling next object
Key: lvl-2-1 Value: lvl-2.1.1
Key: lvl-2-2 Value: lvl-2.1.2
Calling next object
Key: lvl-3 Value: lvl-3.1
Key: lvl-3 Value: lvl-3.2
Calling next object
Key: lvl-4-1 Value: lvl-4.1
Key: lvl-4-2 Value: lvl-4.1.2
Key: lvl-4-1 Value: lvl-4.2
Key: lvl-4-2 Value: lvl-4.2.2
我没有打印所有的键值
组合,我不知道这是否是一个好的解决方案。重要的是,我不要压扁json,因为列表末尾帮助我知道何时执行futur函数。抱歉发了这么长的帖子,谢谢你花时间阅读 简单的解决方案,如果我做对了,你真的只需要一个简单的递归
full_dict = mass_upt_res_data_json["mass_update"]
def check_inst(elem):
for e in elem:
if isinstance(e, list):
print("list reached")
check_inst(e)
else:
for key, value in e.items() :
print (key, value)
check_inst(full_dict)
印刷品:
lvl-1 lvl-1.1
lvl-1 lvl-1.2
lvl-1 lvl-1.3
list reached
lvl-2 lvl-2.1.2
list reached
lvl-3 lvl-3.1
lvl-3 lvl-3.2
list reached
lvl-4 lvl-4.1.2
lvl-4 lvl-4.2.2
lvl-2 lvl-2.2.2
list reached
lvl-3 lvl-3.3
lvl-3 lvl-3.4
list reached
lvl-4 lvl-4.3.2
lvl-4 lvl-4.4.2
哦,哇,谢谢你,我想它能正常工作:)!我真的很感激你花时间来帮助我!