1、數據書庫結構
1 | 家用電器 | 0 | 一級菜單 |
2 | 手機、數碼、京東通信 | 0 | 一級菜單 |
3 | 電腦、辦公 | 0 | 一級菜單 |
4 | 家具、家居、廚房 | 0 | 一級菜單 |
5 | 男裝、女裝、童裝、內衣 | 0 | 一級菜單 |
6 | 個人護裝、清潔用品 | 0 | 一級菜單 |
7 | 大家電 | 1 | 二級菜單 |
8 | 廚衛大電 | 1 | 二級菜單 |
9 | 廚衛小電 | 1 | 二級菜單 |
10 | 生活用品 | 1 | 二級菜單 |
11 | 平板電視 | 7 | 三級菜單 |
12 | 家用空調 | 7 | 三級菜單 |
13 | 油煙機 | 8 | 三級菜單 |
14 | 燃氣灶 | 8 | 三級菜單 |
16 | 電飯煲 | 9 | 三級菜單 |
17 | 微波爐 | 9 | 三級菜單 |
18 | 手機通訊 | 2 | 二級菜單 |
19 | 運營商 | 2 | 二級菜單 |
20 | 京東通信 | 2 | 二級菜單 |
21 | 手機 | 18 | 三級菜單 |
22 | 對講機 | 18 | 三級菜單 |
23 | 手機維修 | 18 | 三級菜單 |
24 | 選號中心 | 20 | 三級菜單 |
25 | 自助服務 | 20 | 三級菜單 |
26 | 電腦整機 | 3 | 二級菜單 |
27 | 電腦配件 | 3 | 二級菜單 |
28 | 外設產品 | 3 | 二級菜單 |
29 | 筆記本 | 26 | 三級菜單 |
30 | 游戲本 | 26 | 三級菜單 |
31 | 平板電腦 | 26 | 三級菜單 |
32 | CPU | 27 | 三級菜單 |
33 | 主板 | 27 | 三級菜單 |
34 | 顯卡 | 27 | 三級菜單 |
36 | 鼠標 | 28 | 三級菜單 |
37 | 鍵盤 | 28 | 三級菜單 |
2、c#代碼

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; using SanxingjinxiaocunDAL; using System.Runtime.Serialization.Json; using System.IO; using System.Text; namespace Sanxingjinxiaocun.qinghua { /// <summary> /// Method 的摘要說明 /// </summary> public class Method : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; #region 調用寫好的類,序列化成JSON格式 Cliet clite = new Cliet(); JieDian root = new JieDian(); root.Name = "根節點"; root.Id = 0; clite.creatTheTree("0", root); //根節點的parentBh值為"0" //對root對象進行序列化 DataContractJsonSerializer dc = new DataContractJsonSerializer(root.GetType()); MemoryStream ms = new MemoryStream(); dc.WriteObject(ms, root); byte[] aryByte = ms.ToArray(); string json = Encoding.UTF8.GetString(aryByte, 0, aryByte.Length); ms.Close(); context.Response.Write(json.ToString()); #endregion } public bool IsReusable { get { return false; } } } #region 根所JSON格式,先寫相應的實體類,為讀取數據后封裝提供支持。 ///<summary> ///節點的實體類,記錄了數據庫中的3個字段 ///為的是方便操作 /// </summary> [Serializable] public class Item { public int Id; public string Name; public int ParentId; public string ContentText; } /// <summary> /// 節點類,基礎類 /// </summary> [Serializable] public class JieDian { public string Name = ""; public int Id = 0; public int ParentId = 0; public string ContentText = ""; public JieDian[] children = null; } /// <summary> /// ss /// </summary> [Serializable] public class Cliet { //根據parentBh獲取相應的子目錄集合 public Item[] GetTheItems(string parentId) { //根據父節點獲取選項集合 string sql = "select * from t_Menu where parentId=" + parentId; //這里改成你的數據庫表 SqlDataReader dr = DbHelperSQL.ExecuteReader(sql); //這里我直接調用了我庫中的類 List<Item> items = new System.Collections.Generic.List<Item>(); while (dr.Read()) { Item i = new Item(); i.Id = dr.GetInt32(0); i.Name = dr.GetString(1); i.ParentId = dr.GetInt32(2); i.ContentText = dr.GetString(3); items.Add(i); } dr.Close(); //一定要對這進行關閉 return items.ToArray(); //返回 } //生成樹的方法 public void creatTheTree(string parentId, JieDian jd) { //獲取 Item[] items = GetTheItems(parentId); //如果沒有字節點了,那就返回空 if (items.Length == 0) return; List<JieDian> jdList = new List<JieDian>(); for (int i = 0; i < items.Length; i++) { JieDian jiedian = new JieDian(); jiedian.Id = items[i].Id; jiedian.Name = items[i].Name; jiedian.ParentId = items[i].ParentId; jiedian.ContentText = items[i].ContentText; //遞歸循環 creatTheTree(items[i].Id.ToString(), jiedian); jdList.Add(jiedian); } jd.children = jdList.ToArray(); //由於對象是引用類型,因為可以改變參數的值 } } #endregion }
3、返回Json:
var data ={ "ContentText": "", "Id": 0, "Name": "根節點", "ParentId": 0, "children": [ { "ContentText": "一級", "Id": 1, "Name": "家電電器", "ParentId": 0, "children": [ { "ContentText": "二級", "Id": 4, "Name": "小米電視", "ParentId": 1, "children": [ { "ContentText": "三級", "Id": 7, "Name": "小米1S", "ParentId": 4, "children": null }, { "ContentText": "三級", "Id": 8, "Name": "小米2S", "ParentId": 4, "children": null }, { "ContentText": "三級", "Id": 9, "Name": "小米3S", "ParentId": 4, "children": null } ] }, { "ContentText": "二級", "Id": 6, "Name": "樂視電視", "ParentId": 1, "children": [ { "ContentText": "三級", "Id": 10, "Name": "超級電視1", "ParentId": 6, "children": null }, { "ContentText": "三級", "Id": 11, "Name": "超級電視2", "ParentId": 6, "children": null } ] } ] }, { "ContentText": "一級", "Id": 3, "Name": "孕婦嬰兒", "ParentId": 0, "children": [ { "ContentText": "二級", "Id": 13, "Name": "孕婦裝", "ParentId": 3, "children": null }, { "ContentText": "二級", "Id": 14, "Name": "孕婦枕", "ParentId": 3, "children": null }, { "ContentText": "二級", "Id": 15, "Name": "孕婦鈣片", "ParentId": 3, "children": null }, { "ContentText": "二級", "Id": 16, "Name": "嬰兒車", "ParentId": 3, "children": [ { "ContentText": "三級", "Id": 19, "Name": "搖搖車", "ParentId": 16, "children": null }, { "ContentText": "三級", "Id": 20, "Name": "木馬車", "ParentId": 16, "children": null } ] }, { "ContentText": "二級", "Id": 18, "Name": "嬰兒奶粉", "ParentId": 3, "children": null } ] } ] }
遞歸循環解析:
$.each(data.children,function(index,item1){ console.log(item1) if(item1.children){ $.each(item1.children,function(index,item2){ console.log(item2) if(item2.children){ $.each(item2.children,function(index,item3){ console.log(item3) }) } }) }