python - 浏览包含嵌套列表和字典的动态 json
问题描述
我有嵌套list
和dictionnary
内部的多级 json 文件,它们看起来像这样,但大小和级别各不相同:
{
"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 和print
每一个key
value
组合。当我在一个嵌套的末尾时list
,我想执行一个function
,让我们暂时说一个 do print("This is the end of the list, yay :-) )"
。
我尝试了一些东西,比如创建一个我存放 my或 myclass
的单曲:var
dictionnary
list
class MassUpt:
def __init__(self, my_dict):
self.my_dict = my_dict
def get_my_dict(self):
return self.my_dict
我obj
从class
内部保存 a global list
:
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()
然后我执行另一个function
,即目前printing
的key value
组合:
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
我没有key value
打印所有组合,我不知道这是否是好的解决方案。重要的是我不要弄平 json,因为列表的末尾可以帮助我知道何时执行 futur function
。抱歉发了这么长的帖子,感谢您抽出宝贵时间阅读!
解决方案
简单的解决方案,如果我做对了,你真的只需要一个简单的递归
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
推荐阅读
- python - Python 已停止工作 (APPCRASH) Anaconda
- .htaccess - 如果 URL 包含特定字符,我如何重定向它
- javascript - 如何使用javascript查找每个字符的像素宽度
- uwp - 如何通过代码从 HoloLens 的设置中调用实用程序页面?
- angular - 如何防止共享模块在 Angular/Webpack 中的延迟加载模块中重复?
- c - Makefile C 包含已安装库的头文件并链接到库的最佳方法
- wordpress - 如何使 is_rtl() 返回为真
- sql - 我可以将 if 条件与存储过程的提取结合起来吗?
- haskell - Haskell中不同类型之间的关系
- java - 如何在特定的 Java 版本上运行应用程序?