2014-03-19 56 views
0

我正在使用jstree插件,我需要json格式,它是內部對象。Json對象內部對象通過asp.net c#從sql服務器

這是我的輸出:

[{"id":"1","text":"Document Management","parent":"#","icon":"fa fa-table","selected":1} 
,{"id":"2","text":"Document List","parent":"1","icon":"fa fa-list","selected":1} 
,{"id":"7","text":"Hazard","parent":"#","icon":"fa fa-file-text","selected":1}] 

這正是我需要的:

[{"id":"1","text":"Document Management","parent":"#","icon":"fa fa-table",state: { opened: true, selected: true }}} 
,{"id":"2","text":"Document List","parent":"1","icon":"fa fa-list",state: { opened: true, selected: true }}} 
,{"id":"7","text":"Hazard","parent":"#","icon":"fa fa-file-text",state: { opened: true, selected: true }}}] 

,這些都是它創建串行化JSON和TreeView我的C#和js代碼; C#

[WebMethod] 
public static string Menu() 
{ 
    ClassSystemAccessPolicy classDocumentPolicy = new ClassSystemAccessPolicy(); 
    DataTable dt = new DataTable(); 
    dt = classDocumentPolicy.Permission_Load().Tables[0]; 

    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); 
    Dictionary<string, object> row; 

    foreach (DataRow dr in dt.Rows) 
    { 
     row = new Dictionary<string, object>(); 
     foreach (DataColumn col in dt.Columns) 
     { 
      row.Add(col.ColumnName, dr[col]); 
     } 
     rows.Add(row); 
    } 
    return serializer.Serialize(rows); 
} 

JS

var MenuTree = function() { 
    $.ajax({ 
     type: "POST", 
     url: "Permission.aspx/Menu", 
     contentType: "application/json", 
     dataType: "json", 
     success: function (data) { 
      var menu$json = JSON.parse(data.d); 
      $('#tree_menu').jstree({ 
       'plugins': ["wholerow", "checkbox", "types"], 
       'core': { 
        "themes": { 
         "responsive": false 
        }, 
        'data': menu$json 
       } 
      }); 
      console.log(menu$json) 
     }, 
     error: function() { 
      console.log('err') 
     } 
    }); 

我怎樣才能像連載state: { selected: true }

+0

爲什麼在世界上確實有.NET「d」返回JSON作爲隨機父節點? – mmcrae

回答

0

您需要創建自己的DTO(數據傳輸對象)這個

public class JSTreeDTO 
{ 
public string id{get;set;} 
public string text{get;set;} 
public string text{get;set;} 
public string parent {get;set;} 
public string icon{get;set;} 
public StateDTO state{get;set;} 
} 

public class StateDTO 
{ 
public bool opened{get;set;} 
public bool selected{get;set;} 
} 

然後創建它的內部循環

List<JSTreeDTO> treeList=new List<JSTreeDTO>(); 

foreach (DataRow dr in dt.Rows) 
{ 
    JSTreeDTO node=new JSTreeDTO(); 
    row = new Dictionary<string, object>(); 
    foreach (DataColumn col in dt.Columns) 
    { 
     node.id=dr["id"];//etc 
     node.state=new StateDTO(); 
     node.state.opened=true;//based on your logic   

    } 
    treeList.Add(node); 

    return serializer.Serialize(treeList); 
} 
+0

它返回'「狀態」:[{「Key」:「opened」,「Value」:true},{「Key」:「slected」,「Value」:true}]}'我刪除了datacolumn foreach,它給了我一個錯誤。我使用'dr [「id」] .toString()' – user3436421

+0

@ user3436421,我改變了我的實現並更新了我的答案 –

+0

它運行良好;) 謝謝兄弟(: – user3436421