有效的json动态创建一个jstree节点

我有一个返回角色列表的方法,我想把这些角色放在jstree中,但我不知道如何。

我试着做以下但我不知道如何为jstree创建一个有效的json

function createNodeList() { $('#processRoleTree').jstree({ "json_data": { "ajax": { "type": "POST", "url": "/TreeLoader.aspx?Action=GetProcessRoles", "dataType": "json", "data": function (n) { return { id: n.attr ? n.attr("id") : 0} } } }, "plugins": ["json_data", "themes", "ui"] }).bind("select_node.jstree", function (e, data) { var selectedObj = data.rslt.obj; alert(selectedObj.attr("id")); }); } 

在TreeLoader.aspx页面加载我有:

 protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["Action"].Equals("GetProcessRoles")) { GetProcessRoles(); } } 

GetProcessRoles是我的方法,它返回ProcessRole对象的列表。

 { "data" : "node_title", // omit `attr` if not needed; the `attr` object gets passed to the jQuery `attr` function "attr" : { "id" : "node_identificator", "some-other-attribute" : "attribute_value" }, // `state` and `children` are only used for NON-leaf nodes "state" : "closed", // or "open", defaults to "closed" "children" : [ /* an array of child nodes objects */ ] } 

如果你使用ajax从服务器获取json,请确保你创建这样的json结构

 [ { "data" : "A node", "children" : [ { "data" : "Only child", "state" : "closed" } ], "state" : "open" }, "Ajax node" ] 

你可以看到这个细节

http://www.jstree.com/documentation/json_data