如何使用ID和PARENTID其他JSON对象创建JSON像一个树形结构 [英] How to create json like a tree structure using id and parentid from other json object

查看:2537
本文介绍了如何使用ID和PARENTID其他JSON对象创建JSON像一个树形结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个JSON:

  [{
            ID:11865,
            PARENTID:空,
            levelid:63,
            名:合计
        },
        {
            ID:10143,
            PARENTID:11865,
            levelid:19,
            名:生产性
        },
        {
            ID:11873,
            PARENTID:10143,
            levelid:20,
            名:常规
        },
        {
            ID:-852,
            PARENTID:11873,
            levelid:255,
            名:801-REGULAR
        },
        {
            ID:-888,
            PARENTID:11873,
            levelid:255,
            名:888-2常规
        }]

和我需要改变它的一些东西像

  [{
  ID:11865,
  levelid:63,
  PARENTID:空,
  名:合计,
  孩子:[{
                ID:10143,
                PARENTID:11865,
                级别:19,
                名:生产性
                孩子:[{
                              ID:10144,
                              PARENTID:10143,
                              levelid:20,
                              名:其他,
                              孩子:[{......}]
             }];

这新的JSON是基于父子关系。另外,我在节点上的js一​​边断绝实施本。可以在这个任何一个帮助吗?


解决方案

这将创建一棵树,并采取一个给定的 PARENTID 作为树的根的建议。该解决方案适用于未排序的数据。


  

它是如何工作:


  
  

基本上为数组中的每个对象需要以及在 ID 为建设一个新的对象为 PARENTID 为一个新的对象


  
  

因此​​,例如

  {ID:6,PARENTID:4}


  
  

首先生成具有 ID 这个属性。

 6:{
    ID:6,
    PARENTID:4
}


  
  

,然后将该属性与 PARENTID

 4:{
    孩子:
        {
            ID:6,
            PARENTID:4
        }
    ]
},

和而所有对象这样的对待,我们终于得到了一棵树。


  
  

如果 PARENTID ===根根节点找到。这是为以后返回的对象。


\r
\r

VAR数据= [{ID:11865,PARENTID:空, levelid:63,名:共},{ID:10143,PARENTID:11865,levelid:19日,名:生产性},{ID:11873, PARENTID:10143,levelid:20日,名:常规},{ID:-852,PARENTID:11873,levelid:255,名:801-REGULAR },{ID:-888,PARENTID:11873,levelid:255,名:888-2常规}],\r
    树=功能(数据,根){\r
        变种R,O = {};\r
        data.forEach(函数(){\r
            a.children = O [a.id&功放;&安培; Ø[a.id]。儿童;\r
            Ø[a.id] = A;\r
            如果(a.parentid ===根){\r
                R = A;\r
            }其他{\r
                Ø[a.parentid] = O [a.parentid] || {};\r
                Ø[a.parentid]。孩子= O [a.parentid]。孩子|| [];\r
                Ø[a.parentid] .children.push(一);\r
            }\r
        });\r
        返回ř;\r
    }(数据,NULL);\r
\r
的document.write('< pre>'+ JSON.stringify(树,0,4)+'< / pre>');

\r

\r
\r

I have a json like:

[{
            "id": 11865,
            "parentid": null,
            "levelid": 63,
            "name": "Total"
        },
        {
            "id": 10143,
            "parentid": 11865,
            "levelid": 19,
            "name": "Productive"
        },
        {
            "id": 11873,
            "parentid": 10143,
            "levelid": 20,
            "name": "Regular"
        },
        {
            "id": -852,
            "parentid": 11873,
            "levelid": 255,
            "name": "801-REGULAR"
        },
        {
            "id": -888,
            "parentid": 11873,
            "levelid": 255,
            "name": "888-Regular 2"
        }]

And I need to change it some thing like that

[{
  "id":11865,
  "levelid":63,
  "parentid": null,
  "name": "Total",
  "children":[{ 
                "id":10143, 
                "parentid":11865, 
                "level":19, 
                "name": "Productive"
                "children":[{
                              "id": 10144,
                              "parentid":10143,
                              "levelid":20,
                              "name": "Other",
                              "children":[{......}]
             }];

This new json is based on parent child relation. Also I am implementing this on sever side in node js. Can Any one help on this ?

解决方案

A proposal which creates a tree and takes a given parentid as a root for the tree. This solution works for unsorted data.

How it works:

Basically for every object in the array it takes as well the id for building a new object as the parentid for a new object.

So for example

{ "id": 6, "parentid": 4 }

it generates first with id this property

"6": {
    "id": 6,
    "parentid": 4
}

and then this property with parentid

"4": {
    "children": [
        {
            "id": 6,
            "parentid": 4
        }
    ]
},

and while all object treated like this, we finally get a tree.

If parentid === root the root node is found. This is the object for the later return.

var data = [{ "id": 11865, "parentid": null, "levelid": 63, "name": "Total" }, { "id": 10143, "parentid": 11865, "levelid": 19, "name": "Productive" }, { "id": 11873, "parentid": 10143, "levelid": 20, "name": "Regular" }, { "id": -852, "parentid": 11873, "levelid": 255, "name": "801-REGULAR" }, { "id": -888, "parentid": 11873, "levelid": 255, "name": "888-Regular 2" }],
    tree = function (data, root) {
        var r, o = {};
        data.forEach(function (a) {
            a.children = o[a.id] && o[a.id].children;
            o[a.id] = a;
            if (a.parentid === root) {
                r = a;
            } else {
                o[a.parentid] = o[a.parentid] || {};
                o[a.parentid].children = o[a.parentid].children || [];
                o[a.parentid].children.push(a);
            }
        });
        return r;
    }(data, null);

document.write('<pre>' + JSON.stringify(tree, 0, 4) + '</pre>');

这篇关于如何使用ID和PARENTID其他JSON对象创建JSON像一个树形结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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