首页 > 解决方案 > 浏览包含嵌套列表和字典的动态 json

问题描述

我有嵌套listdictionnary内部的多级 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的单曲:vardictionnarylist

class MassUpt:
def __init__(self, my_dict):
    self.my_dict = my_dict

def get_my_dict(self):
    return self.my_dict

objclass内部保存 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,即目前printingkey 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。抱歉发了这么长的帖子,感谢您抽出宝贵时间阅读!

标签: pythonjsonpython-3.xlistdictionary

解决方案


简单的解决方案,如果我做对了,你真的只需要一个简单的递归

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

推荐阅读