本文介绍了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": []
        }
]

要打印从根到叶的所有路径(其中一个空的子级" ). 例如Basic.DeviceInfo.DeviceInfo Form.DeviceInfo Form1

Want to print all paths from root to leaf(one with empty 'childrens'). E.g Basic.DeviceInfo.DeviceInfo Form.DeviceInfo Form1

一切正常,直到 DeviceInfo Form2

当涉及到 DeviceInfo Table (设备信息表)时,出现了 DeviceInfo表单-> Basic.DeviceInfo.DeviceInfo Form.DeviceInfo Table.DeviceInfo Table1.

When it comes to DeviceInfo Table, DeviceInfo Form is coming into picture --> Basic.DeviceInfo.DeviceInfo Form.DeviceInfo Table.DeviceInfo Table1.

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

This should not happen. Instead I need Basic.DeviceInfo.DeviceInfo Table.DeviceInfo Table1.

我的代码在哪里出问题了.有解决办法吗?

Where am I going wrong with my code. Any solution?

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)

推荐答案

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

You are setting your path = path +dic['name']+'.' in your else clause. Once the walk() function has finished traversing through the DeviceInfoForm 'childrens', it attempts to traverse the DeviceInfoTable. However, your function has already set path toBasic.DeviceInfo.DeviceInfoForm.

您需要重新组织功能,以便在else:语句中未设置路径.也许

You need to reorganize your function so that the path isn't set in the else: statement. Perhaps

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

通过这种方式,您将正确的路径传递给函数,但未明确设置它.

That way you are passing the correct path to the function, but not explicitly setting it.

这篇关于JSON打印从根到叶的所有路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 01:26