简体   繁体   English

遍历JSON对象以创建jsTree

[英]Traverse JSON object to create jsTree

I want to create a jsTree from JSON with different depth (could be 10 or more levels). 我想从JSON创建一个具有不同深度(可以是10个或更多级别)的jsTree。 So I searched the web and found a method to create the jsTree: traversing. 因此,我在网上搜索并找到了一种创建jsTree的方法:遍历。

Example JSON: JSON示例:

{
    "testslave_1": {
        "DevKey": "94ssfsafafeaw382",
        "mapping": {
            "device1": "D1",
            "device2": "D2",
            "device3": "D3"
        }
    }
}

Lucky for me there is traverse-js but I have problems with it. 对我来说幸运的是有遍历js,但是我有问题。

This is the syntax required by jsTree: 这是jsTree所需的语法:

$('#using_json_2').jstree({ 'core' : {
    'data' : [
       { "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
       { "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
       { "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
       { "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
    ]
} });

I have tried this so far: 到目前为止,我已经尝试过了:

var obj = '{    "testslave_1": {        "DevKey": "94ssfsafafeaw382",       "mapping": {            "device1": "D1",            "device2": "D2",            "device3": "D3"     }   }}';

    var obj = JSON.parse(obj);
    var arr = [];

    traverse(obj).map(function (x) {

    var jsonTemp = '{ "id" : "'+this.path+'", "parent" : "'+this.parent+'", "text" : "'+this.node+'" }';
    jsonTemp = JSON.parse(jsonTemp);
    arr.push(jsonTemp);

    });

    console.dir(arr);

But there are serveral problems: How to create an specific ID for each item? 但是存在服务器方面的问题:如何为每个项目创建一个特定的ID? (the function may remember the id for the parent-attribute) (该函数可能会记住父属性的ID)

It says object object, if I understand it in the right way I have to access the node of the parent object. 它说的是对象对象,如果我以正确的方式理解它,则必须访问父对象的节点。 But how? 但是如何? this.parent.node does not work. this.parent.node不起作用。

There are items missing, it should be around 10 items. 缺少物品,应该大约有10件。

Is my way of the solution going in the right direction? 我的解决方案方向正确吗?

Thank you in advance. 先感谢您。

I have no idea of traverse-js. 我不知道traverse-js。 If you want to get some kind of tree structure like the example you can achieve that with a recursive function, for example like this one: 如果您想像示例一样获得某种树形结构,则可以使用递归函数来实现,例如:

var testobj = {
    testslave_1:{
        DevKey: "94ssfsafafeaw382",
        mapping: {
            device1:"D1",
            device2:"D2",
            device3:"D3"
        }
    }
};
var tree = {
    core: {
        data: []
    }
};
function createTreeData(object, parent){
    if(!parent){
        parent = "#"
    }
    for(var prop in object){
        if(object.hasOwnProperty(prop)){
            var obj = {
                id: prop,
                parent: parent
            };
            if(typeof object[prop] === "object"){
                createTreeData(object[prop], prop);
                obj.text = "parent node";
            }else{
                obj.text = object[prop];
            }
            tree.core.data.push(obj);
        }
    }
}
createTreeData(testobj);
console.log(tree);

The only thing which this function doesn't provide is text for the elements which are parentNodes themselves. 此功能唯一不提供的是元素的文本,这些元素本身就是parentNodes。 Instead it puts in the child objects which is most likely wrong. 相反,它放入最有可能出错的子对象。 But you didn't specify what text parent nodes should contain. 但是您没有指定父节点应包含的文本。

EDIT: I changed the function so that parent nodes now contain the text "parent node" rather than their child object. 编辑:我更改了功能,以便父节点现在包含文本“父节点”而不是其子对象。

EDIT2: New function which makes nodes for properties which are just text: EDIT2:新功能使仅文本属性的节点:

function createTreeData(object, parent){
    if(!parent){
        parent = "#"
    }
    for(var prop in object){
        if(object.hasOwnProperty(prop)){
            tree.core.data.push({
                id: prop,
                parent: parent,
                text: prop
            });
            if(typeof object[prop] === "object"){
                createTreeData(object[prop], prop);
            }else{
                tree.core.data.push({
                    id: object[prop],
                    parent: prop,
                    text: object[prop]
                });
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM