JSON 打印从根到叶的所有路径

tez*_*tez 2 python json

[
        {
            "name": "Basic",
            "id": "home",
            "childrens": [
                {
                    "name": "Dashboard",
                    "viewtype": "custom",
                    "view": "dashboard.html",
                    "childrens": []
                },
                {
                    "name": "DeviceInfo",
                    "href": "WSettings",
                    "childrens": [
                        {
                            "name": "DeviceInfo Form",
                            "childrens": [
                                {
                                    "name": "DeviceInfo Form1",
                                    "viewtype": "xml",
                                    "view": "dinfo",
                                    "childrens": []
                                },
                                {
                                    "name": "DeviceInfo Form2",
                                    "viewtype": "xml",
                                    "view": "complexjson",
                                    "childrens": []
                                }
                            ]
                        },
                        {
                            "name": "DeviceInfo Table",
                            "childrens": [
                                {
                                    "name": "DeviceInfo Table1",
                                    "viewtype": "xml",
                                    "view": "dinfotable",
                                    "childrens": []
                                },
                                {
                                    "name": "DeviceInfo Table2",
                                    "viewtype": "xml",
                                    "view": "jsontable",
                                    "childrens": []
                                }
                            ]
                        }

                    ]
                },
                {
                    "name": "Hybrid",
                    "childrens": [
                        {
                            "name": "Table-Form",
                            "viewtype": "xml",
                            "view": "hybrid",
                            "childrens": []
                        }
                    ]
                }
            ]
        },
        {
            "name": "Advanced",
            "id": "profile",
            "childrens": []
        }
]
Run Code Online (Sandbox Code Playgroud)

想要打印从根到叶的所有路径(一个带有空的'childrens')。例如 Basic.DeviceInfo.DeviceInfo Form.DeviceInfo Form1

一切顺利,直到DeviceInfo Form2

当谈到DeviceInfo Table 时DeviceInfo Form就出现了 --> Basic.DeviceInfo.DeviceInfo Form.DeviceInfo Table.DeviceInfo Table1。

这不应该发生。相反,我需要Basic.DeviceInfo.DeviceInfo Table.DeviceInfo Table1。

我的代码哪里出错了。有什么解决办法吗?

def walk(list1, path = ""):
        for dic in list1:
            #print('about to walk', dic['name'], 'passing path -->', path)
            if(len(dic['childrens']) == 0):
                print('leaf --->', path+dic['name']+'.')
            else:
                path = path+dic['name']+'.'
                #passing parent name to childreni
                walk(dic['childrens'], path)
Run Code Online (Sandbox Code Playgroud)

Tui*_*noe 5

你正在path = path +dic['name']+'.'你的 else 子句中设置你的。一旦walk()函数完成遍历 DeviceInfoForm 'childrens',它就会尝试遍历 DeviceInfoTable。但是,您的函数已经将路径设置为 Basic.DeviceInfo.DeviceInfoForm.

您需要重新组织您的函数,以便在else:语句中不设置路径。也许

else:
    walk(dic['childrens'], path+dic['name']+'.')
Run Code Online (Sandbox Code Playgroud)

这样你就将正确的路径传递给函数,但没有明确设置它。