Javascript - JSON:构建分层树

时间:2014-07-05 06:57:45

标签: javascript json

我正在尝试使用dhtmlxtreegrid显示树

我需要帮助从拼合的JSON创建分层树数据。

我的JSON数据如下所示:

{
    "rows":  [
        {
            "id": "01", // child id
            "parentid": "00", // parent Id
            "data": [
                "101831",
                "Work",
                "Desc-4-1"
            ]
        },
        {
            "id": "02",
            "parentid": "01",
            "data": [
                "101832",
                "No Work",
                "Desc-4-0"
            ]
        },
        {
            "id": "03",
            "parentid": "01",
            "data": [
                "101835",
                "Work",
                "Desc-5-0"
            ]
        },
        {
            "id": "04",
            "parentid": "03",
            "data": [
                "101835",
                "Work",
                "Desc-5-1"
            ]
        }
    ]
}

结果JSON应该是分层树结构:

{
    "rows": [
        {
            "id": "01",
            "parentid": "00",
            "data": [
                "101831",
                "Work",
                "Desc-4-1"
            ],
            "rows": [
                {
                    "id": "02",
                    "parentid": "01",
                    "data": [
                        "101832",
                        "No Work",
                        "Desc-4-0"
                    ]
                },
                {
                    "id": "0",
                    "parentid": "01",
                    "data": [
                        "101835",
                        "Work",
                        "Desc-5-0"
                    ]
                }
            ]
        }
    ]
}

2 个答案:

答案 0 :(得分:0)

如果你需要一个c#解决方案,这里有一个为你生成c#代码的链接。

http://json2csharp.com/

取决于你的结果这里的JSON数据是c#代码:

public class Row2
{
    public string id { get; set; }
    public string parentid { get; set; }
    public List<string> data { get; set; }
}

public class Row
{
    public string id { get; set; }
    public string parentid { get; set; }
    public List<string> data { get; set; }
    public List<Row2> rows { get; set; }
}

public class RootObject
{
    public List<Row> rows { get; set; }
}

答案 1 :(得分:-1)

如果我能正确理解你的问题,我已经尝试了多项工作。

您的JSON数据: -

var data = {
"rows":  [
    {
        "id": "01", // child id
        "parentid": "00", // parent Id
        "data": [
            "101831",
            "Work",
            "Desc-4-1"
        ]
    },
    {
        "id": "02",
        "parentid": "01",
        "data": [
            "101832",
            "No Work",
            "Desc-4-0"
        ]
    },
    {
        "id": "03",
        "parentid": "01",
        "data": [
            "101835",
            "Work",
            "Desc-5-0"
        ]
    }
]
}

改变结构的功能。

var addRowsToLastChild = function(obj1,rowsToAppend){
//if that object has rows key check in that row otherwise add rows to the object
    if(obj1["rows"]){  
        addRowsToLastChild(obj1["rows"][0],rowsToAppend);
        return;
    }
    obj1["rows"] = [];
    obj1["rows"][0] = rowsToAppend;
    obj1;
}

var resultJSON = data.rows.reduce(function(obj1,obj2){
    addRowsToLastChild(obj1,obj2); 
    // object are passed by refference so any changes in obj1 will be affected
    return obj1;
},{});

结果将是: -

{
"rows": [
    {
        "id": "01",
        "parentid": "00",
        "data": [
            "101831",
            "Work",
            "Desc-4-1"
        ],
        "rows": [
            {
                "id": "02",
                "parentid": "01",
                "data": [
                    "101832",
                    "No Work",
                    "Desc-4-0"
                ],
                "rows": [{
                "id": "03",
                "parentid": "01",
                "data": [
                    "101835",
                    "Work",
                    "Desc-5-0"
                ]
            }
            ]
            }

        ]
    }
]
};