JSON打印来自的所有路径

2024-09-27 09:35:18 发布

您现在位置:Python中文网/ 问答频道 /正文

[
        {
            "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": []
        }
]

想要打印从根到叶的所有路径(其中一个路径的“childrens”为空)。例如基本.DeviceInfo.DeviceInfo窗体设备信息表格1

一切都很顺利,直到DeviceInfo Form2

当谈到设备信息表时,设备信息表就进入了画面-->基本.DeviceInfo.DeviceInfo窗体设备信息设备信息表表1。

这不应该发生。相反,我需要基本.DeviceInfo.DeviceInfo设备信息表表1。

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

^{pr2}$

Tags: name路径formview信息idhomebasic
1条回答
网友
1楼 · 发布于 2024-09-27 09:35:18

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

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

else:
    walk(dic['childrens'], path+dic['name']+'.')

这样您就可以将正确的路径传递给函数,而不是显式地设置它。在

相关问题 更多 >

    热门问题