【问题标题】:Loop through a multilevel JSON object having children of same name循环遍历具有同名子级的多级 JSON 对象
【发布时间】:2013-06-03 12:47:27
【问题描述】:

我有一个 JSON 对象如下。

{
    "name":"Me",
    "groups": [
        {
            "name":"My Garage",
            "groups":[
                {
                    "name":"My Cars",
                    "groups":[],
                    "tags":[
                        {
                            "name":"Fiet Punto"
                        },
                        {
                            "name":"Gallardo"
                        }
                    ]
                },
                {
                    "name":"My Bikes",
                    "groups":[]
                }
            ]
        },
        {
            "name":"My Bank",
            "groups":[
                {
                    "name":"Swiss Bank",
                    "groups":[]
                },
                {
                    "name":"Bank of America",
                    "groups":[],
                    "tags":[
                        {
                            "name":"Account 1"
                        },
                        {
                            "name":"Account 2"
                        }
                    ]
                }
            ]
        }
    ],
    "tags":[
        {
            "name":"My tag 1"
        },
        {
            "name":"My tag 2"
        }
    ]
}

我想要的输出如下:

Me
--My Garage
  --My Cars
    --Fiet Punto
    --Gallardo
  --My Bikes
--My Bank
  --Swiss Bank
  --Bank of America
    -- Account 1
    -- Account 2
--My Tag 1
--My Tag 2

我在构建递归时遇到问题。我想做的是:

  • 获取根对象。
  • 打印name
  • 查找对象中是否存在groupstags
  • 如果其中任何一个存在,则循环遍历内部对象。
  • 应用正确的缩进并按照上述步骤处理内部对象。

我怎样才能做到这一点?

编辑:

function getAllGroups(jsonObject)
{

    //print the name of the current level object.
    $("body").append(jsonObject.name);

    //now if this object contains groups
    if( jsonObject.hasOwnProperty( 'groups' ) )
    {
        //and if it is an array
        if( jsonObject.groups.length > 0 )
        {
            //then for each object in groups of this object, call the function again.
            $(jsonObject.groups).each(function(i, innerObject){
                //print the index for debugging.
                console.log(i + innerObject.name);
                //make a recursive call to the function.
                getAllGroups(innerObject);

            });

        }

    }
    else
    {
        console.log("does not exist anymore.")
    }
}

我无法弄清楚如何同时评估 tagsgroups 并打印保持级别的名称。

更准确地说,我无法弄清楚如何获得树的级别。

【问题讨论】:

  • 请贴出您目前尝试过的代码。您究竟遇到了什么问题?

标签: jquery json parsing


【解决方案1】:

要获取树的级别,我认为最简单的方法就是将当前级别传递给方法:

function getAllGroups(jsonObject,currentlevel)
{

    //print the name of the current level object.
    $("body").append(jsonObject.name);

    //now if this object contains groups
    if( jsonObject.hasOwnProperty( 'groups' ) )
    {
        //and if it is an array
        if( jsonObject.groups.length > 0 )
        {
            var nextlevel = currentlevel+1;
            //then for each object in groups of this object, call the function again.
            $(jsonObject.groups).each(function(i, innerObject){
                //print the index for debugging.
                console.log(i + innerObject.name);
                //make a recursive call to the function.
                getAllGroups(innerObject,nextlevel);

            });

        }

    }
    else
    {
        console.log("does not exist anymore.")
    }
}

通过启动第一级来使用它:

getAllGroups(yourJSON,1);

【讨论】:

  • 这样做了!但没有帮助..没有得到适当的水平
  • @pjp:想法是将当前级别传递给方法。如何管理当前级别取决于您。上面的代码只是一个例子。好吧,但我认为它应该符合您的要求。你能再检查一下是否还有其他问题吗?希望您能尽快找到答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多