你能告诉我如何从树视图制作json对象。我正在使用jstree。我需要在用户"得到json"时得到json对象。对象
这是我预期的演示版: http://jsfiddle.net/fuu94/54/
[
{
"id" : "a",
"children" : []
},
{
"id" : "b",
"children" : [ {"id":"b-a-1", "children" : []}, {"id":"b-a-2", "children" : {"id":"b-b-a", "children" : []},{"id":"b-b-b", "children" : []}}]
},
{
"id" : "c",
"children" : [ {"id":"c-a-1", "children" : []}, {"id":"c-b-2", "children" : []}]
},
]
答案 0 :(得分:0)
谢谢你的downvotes:P
function jsonify() {
var $this = $(this);
return {
id: $this.attr('id'),
children: $this.find('ul > li').map(jsonify).get()
};
}
$('#json').click(function(){
var json_list = $('#tree > ul > li').map(jsonify).get();
console.log(JSON.stringify(json_list));
});
这对我有用,但不适用于那个树插件,因为它改变了DOM!