Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 浏览包含嵌套列表和字典的动态json_Python_Json_Python 3.x_List_Dictionary - Fatal编程技术网

Python 浏览包含嵌套列表和字典的动态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" }, {

我有多层json文件,里面嵌套了
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

哦,哇,谢谢你,我想它能正常工作:)!我真的很感激你花时间来帮助我!