c# 使用递归 循环遍历导航树结构 并解析
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) }) } }) }