简体   繁体   English

使用子项创建json树结构

[英]Create json tree structure with children

I have a json tree structure that is appended to by pressing invoke on this fiddle : http://jsfiddle.net/adrianjsfiddlenetuser/C6Ssa/4/ 我有一个json树结构,通过按下这个小提琴上的调用来附加: http//jsfiddle.net/adrianjsfiddlenetuser/C6Ssa/4/

Press invoke multiple tiles on the fiddle & copy/paste the produced jSon intohttp://jsonlint.com/, the produced json is not valid 按调用小提琴上的多个图块并将生成的jSon复制/粘贴到http://jsonlint.com/,生成的json无效

I need to produce this : 我需要制作这个:

{
    "nodes": [
        {
            "url": "asdfas",
            "date": ""
        },
        {
            "url": "asdfas",
            "date": ""
        },
        {
            "url": "asdfasfdasas",
            "date": ""
        }
    ]
}

Can this be amended so that multiple children can be added to the tree structure, I think I need to amend the var data somehow ? 可以修改这个以便可以将多个子项添加到树结构中,我想我需要以某种方式修改var数据吗?

Try: 尝试:

var data = {nodes: []};

$("#add").on('click', function () {
    data.nodes.push({
        url: "some url",
        date: new Date
    });

    $("#myDiv").text(JSON.stringify(data));
});

if not, I didn't understand your question ;) 如果没有,我不明白你的问题;)

http://jsfiddle.net/gY5yQ/ http://jsfiddle.net/gY5yQ/

See if this helps http://jsfiddle.net/C6Ssa/12/ 看看这有助于http://jsfiddle.net/C6Ssa/12/

var data = [];


$("#add").click(add);
function add() {

data.push({
    param1: "stuff",
    param2:1,
    param3:1
});
var sample = {};
sample.node = data
$("#myDiv").text(JSON.stringify(sample));
}

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

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