创建父子树JSON [英] Create Parent-Child tree JSON

查看:46
本文介绍了创建父子树JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含moduleId和parentId的JSON数据,我想要一个具有模块父-子映射结构的新JSON.

I have a JSON data containing moduleId and parentId, I want a new JSON with a map structure of module's parent-child.

** parentId可以为null,表示该模块没有父级.

**parentId can be null which means that module has no parent.

[{
    "moduleId": 1,
    "parentId": null    
 },
 {
    "moduleId": 2,
    "parentId": 1   
 },
 {
    "moduleId": 3,
    "parentId": 1   
 },
 {
    "moduleId": 4,
    "parentId": null    
 },
 {
    "moduleId": 5,
    "parentId": null    
 },
 {
    "moduleId": 6,
    "parentId": 3   
 }
]

必需的JSON-

[{
    "moduleId": 1,
    "child": [{
                "moduleId": 2,
                "child": []
              },
              {
                "moduleId": 3,
                "child":[{
                            "moduleId": 6,
                            "child": []
                        }]
              }]    
 },
 {
    "moduleId": 4,
    "child": []
 },
 {
    "moduleId": 5,
    "child": []
 }
]

当前代码

问题-孩子内部的孩子没有被映射

Current Code

Problem - children inside children are not getting mapped

function tree($list)
{
    $map = new stdClass();
    $roots = [];$node = [];

    for($i=0; $i<sizeof($list); $i++)
    {
        $c=$list[$i]["moduleId"];
        $map->$c = $i; 

        $list[$i]["children"] = []; // initialize the children
    }

    for($i=0; $i<sizeof($list); $i++)
    {
        $node = $list[$i];
        if( $node["parentId"] !== "" )
        {
            $m = $node["parentId"];
            $val = $map->$m;

            $child = new StdClass;
            $child->moduleId = $node["moduleId"];
            $child->parentId = $node["parentId"];
            $child->children = [];

            array_push($list[$val]["children"],$node);

        }
    }

for($i = 0;$i<sizeof($list);$i++)
    {
        if($list[$i]["parentId"] == "")
        {
            array_push($roots, $list[$i]);
        }
    }
    return json_encode($roots);
}

我不知道.这里的所有答案都给出不同类型的结果. 可以使用任何语言(java,C,php,js,python等).谢谢.

I cant figure it out. All the answers here give different kinds of result. Can use any language(java,C,php,js,python,etc). Thankyou.

推荐答案

var arry = [{
    "parentId": null,
    "moduleId": 1

 },
 {
    "parentId": 1,
    "moduleId": 2

 },
 {
    "parentId": 1,

    "moduleId": 3

 },
 {
    "parentId": null,
    "moduleId": 4

 },
 {
    "parentId": null,
    "moduleId": 5   
 },
 {

    "parentId": 3, 
    "moduleId": 6 
 }
];

function convert(array){
    var map = {};
    for(var i = 0; i < array.length; i++){
        var obj = array[i];
        obj.children= [];

        map[obj.moduleId] = obj;

        var parent = obj.parentId || '-';
        if(!map[parent]){
            map[parent] = {
                children: []
            };
        }
        map[parent].children.push(obj);
    }
    return map['-'].children;
}

var r = convert(arry)
console.log('array', r);
console.log('result', JSON.stringify(r))

这篇关于创建父子树JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆