使用javascript / jquery从平面json创建嵌套json

时间:2015-03-24 12:10:11

标签: javascript jquery json

我当前的json(我为生成树结构而创建的 )如下:

[{"text":"glossary","id":"599","parentid":"-1"},
{"text":"title","id":"600","parentid":"599"},
{"text":"","id":"601","parentid":"600"},
{"text":"GlossDiv","id":"602","parentid":"599"},
{"text":"GlossList","id":"603","parentid":"602"},
{"text":"GlossEntry","id":"604","parentid":"603"},
{"text":"GlossTerm","id":"605","parentid":"604"},
{"text":"Standard Generalized Markup Language","id":"606","parentid":"605"},
{"text":"GlossSee","id":"607","parentid":"604"},
{"text":"markup","id":"608","parentid":"607"},
{"text":"SortAs","id":"609","parentid":"604"},
{"text":"SGML","id":"610","parentid":"609"},
{"text":"GlossDef","id":"611","parentid":"604"},
{"text":"para","id":"612","parentid":"611"},
{"text":"","id":"613","parentid":"612"},
{"text":"GlossSeeAlso","id":"614","parentid":"611"},
{"text":"","id":"615","parentid":"614"},
{"text":"XML","id":"616","parentid":"614"},
{"text":"ID","id":"617","parentid":"604"},
{"text":"SGML","id":"618","parentid":"617"},
{"text":"Acronym","id":"619","parentid":"604"},
{"text":"SGML","id":"620","parentid":"619"},
{"text":"Abbrev","id":"621","parentid":"604"},
{"text":"ISO 8879:1986","id":"622","parentid":"621"},
{"text":"title","id":"623","parentid":"602"},
{"text":"","id":"624","parentid":"623"}]`

如何在Javascript中为上述数组生成嵌套json

1 个答案:

答案 0 :(得分:1)

您可以创建查找字典,保留父项,并在迭代所有条目时引用它们。

var dictionary = {};
for (var i = 0; i < data.length; i++) {
    dictionary[data[i].id] = data[i];
}
for (var i = 0; i < data.length; i++) {
    if (data[i].parentid) {
        var parent = dictionary[data[i].parentid];
        if (parent) {
            if (!parent.children) {
                parent.children = [];
            }
            parent.children.push(data[i]);
        }
    }
}

JSFiddle: http://jsfiddle.net/TrueBlueAussie/y22ctL8o/1/

结果是这样的结构:

[{
    "text": "glossary",
    "id": "599",
    "parentid": "-1",
    "children": [{
        "text": "title",
        "id": "600",
        "parentid": "599",
        "children": [{
            "text": "",
            "id": "601",
            "parentid": "600"
        }]
    }, {
        "text": "GlossDiv",
        "id": "602",
        "parentid": "599",
        "children": [{
            "text": "GlossList",
            "id": "603",
            "parentid": "602",
            "children": [{
                "text": "GlossEntry",
                "id": "604",
                "parentid": "603",
                "children": [{
                    "text": "GlossTerm",
                    "id": "605",
                    "parentid": "604",
                    "children": [{
                        "text": "Standard Generalized Markup Language",
                        "id": "606",
                        "parentid": "605"
                    }]
                }, {
                    "text": "GlossSee",
                    "id": "607",
                    "parentid": "604",
                    "children": [{
                        "text": "markup",
                        "id": "608",
                        "parentid": "607"
                    }]
                }, {
                    "text": "SortAs",
                    "id": "609",
                    "parentid": "604",
                    "children": [{
                        "text": "SGML",
                        "id": "610",
                        "parentid": "609"
                    }]
                }, {
                    "text": "GlossDef",
                    "id": "611",
                    "parentid": "604",
                    "children": [{
                        "text": "para",
                        "id": "612",
                        "parentid": "611",
                        "children": [{
                            "text": "",
                            "id": "613",
                            "parentid": "612"
                        }]
                    }, {
                        "text": "GlossSeeAlso",
                        "id": "614",
                        "parentid": "611",
                        "children": [{
                            "text": "",
                            "id": "615",
                            "parentid": "614"
                        }, {
                            "text": "XML",
                            "id": "616",
                            "parentid": "614"
                        }]
                    }]
                }, {
                    "text": "ID",
                    "id": "617",
                    "parentid": "604",
                    "children": [{
                        "text": "SGML",
                        "id": "618",
                        "parentid": "617"
                    }]
                }, {
                    "text": "Acronym",
                    "id": "619",
                    "parentid": "604",
                    "children": [{
                        "text": "SGML",
                        "id": "620",
                        "parentid": "619"
                    }]
                }, {
                    "text": "Abbrev",
                    "id": "621",
                    "parentid": "604",
                    "children": [{
                        "text": "ISO 8879:1986",
                        "id": "622",
                        "parentid": "621"
                    }]
                }]
            }]
        }, {
            "text": "title",
            "id": "623",
            "parentid": "602",
            "children": [{
                "text": "",
                "id": "624",
                "parentid": "623"
            }]
        }]
    }]
}, {
    "text": "title",
    "id": "600",
    "parentid": "599",
    "children": [{
        "text": "",
        "id": "601",
        "parentid...rkup",
        "id": "608",
        "parentid": "607"
    }]
}, {
    "text": "markup",
    "id": "608",
    "parentid": "607"
}, {
    "text": "SortAs",
    "id": "609",
    "parentid": "604",
    "children": [{
        "text": "SGML",
        "id": "610",
        "parentid": "609"
    }]
}, {
    "text": "SGML",
    "id": "610",
    "parentid": "609"
}, {
    "text": "GlossDef",
    "id": "611",
    "parentid": "604",
    "children": [{
        "text": "para",
        "id": "612",
        "parentid": "611",
        "children": [{
            "text": "",
            "id": "613",
            "parentid": "612"
        }]
    }, {
        "text": "GlossSeeAlso",
        "id": "614",
        "parentid": "611",
        "children": [{
            "text": "",
            "id": "615",
            "parentid": "614"
        }, {
            "text": "XML",
            "id": "616",
            "parentid": "614"
        }]
    }]
}, {
    "text": "para",
    "id": "612",
    "parentid": "611",
    "children": [{
        "text": "",
        "id": "613",
        "parentid": "612"
    }]
}, {
    "text": "",
    "id": "613",
    "parentid": "612"
}, {
    "text": "GlossSeeAlso",
    "id": "614",
    "parentid": "611",
    "children": [{
        "text": "",
        "id": "615",
        "parentid": "614"
    }, {
        "text": "XML",
        "id": "616",
        "parentid": "614"
    }]
}, {
    "text": "",
    "id": "615",
    "parentid": "614"
}, {
    "text": "XML",
    "id": "616",
    "parentid": "614"
}, {
    "text": "ID",
    "id": "617",
    "parentid": "604",
    "children": [{
        "text": "SGML",
        "id": "618",
        "parentid": "617"
    }]
}, {
    "text": "SGML",
    "id": "618",
    "parentid": "617"
}, {
    "text": "Acronym",
    "id": "619",
    "parentid": "604",
    "children": [{
        "text": "SGML",
        "id": "620",
        "parentid": "619"
    }]
}, {
    "text": "SGML",
    "id": "620",
    "parentid": "619"
}, {
    "text": "Abbrev",
    "id": "621",
    "parentid": "604",
    "children": [{
        "text": "ISO 8879:1986",
        "id": "622",
        "parentid": "621"
    }]
}, {
    "text": "ISO 8879:1986",
    "id": "622",
    "parentid": "621"
}, {
    "text": "title",
    "id": "623",
    "parentid": "602",
    "children": [{
        "text": "",
        "id": "624",
        "parentid": "623"
    }]
}, {
    "text": "",
    "id": "624",
    "parentid": "623"
}]

但这完全取决于你打算如何处理结果。这个问题目前很模糊。

注意:上述data的双重迭代允许父母在子女后出现(可能是也可能不是)。如果父母总是首先出现,你可以将逻辑组合成一个循环。

e.g。

var dictionary = {};
for (var i = 0; i < data.length; i++) {
    dictionary[data[i].id] = data[i];
    if (data[i].parentid) {
        var parent = dictionary[data[i].parentid];
        if (parent) {
            if (!parent.children) {
                parent.children = [];
            }
            parent.children.push(data[i]);
        }
    }
}