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 = ;
clite.creatTheTree("", 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, , 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 = ;
public int ParentId = ;
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();
i.Name = dr.GetString();
i.ParentId = dr.GetInt32();
i.ContentText = dr.GetString();
items.Add(i);
}
dr.Close();
//一定要对这进行关闭
return items.ToArray();
//返回
} //生成树的方法
public void creatTheTree(string parentId, JieDian jd)
{
//获取
Item[] items = GetTheItems(parentId);
//如果没有字节点了,那就返回空
if (items.Length == )
return;
List<JieDian> jdList = new List<JieDian>();
for (int i = ; 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)
})
}
})
}
c# 使用递归 循环遍历导航树结构 并解析的更多相关文章
- Atitit 循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate).
Atitit 循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 1.1. 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称之为循环. ...
- 循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate)的区别
表示“重复”这个含义的词有很多, 比如循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称 ...
- 003_循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate)的区别
表示“重复”这个含义的词有很多, 比如循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称 ...
- DOM 元素的循环遍历
博客地址:https://ainyi.com/89 获取 DOM 元素的几种方式 get 方式: getElementById getElementsByTagName getElementsBy ...
- Visual Studio 2010下ASPX页面的TreeView控件循环遍历
如果维护一个老系统就总会遇到各种问题,而这次是TreeView的循环遍历.对于Visual Studio2010上aspx页面的TreeView控件,我感受到了什么叫集微软之大智慧.与二叉树型不一样. ...
- php用压栈的方式,循环遍历无限级别的数组(非递归方法)
php用压栈的方式,循环遍历无限级别的数组(非递归方法) 好久不写非递归遍历无限级分类...瞎猫碰到死老鼠,发刚才写的1段代码,压栈的方式遍历php无限分类的数组... php压栈的方式遍历无限级别数 ...
- json原理和jquey循环遍历获取所有页面元素
1.json原理: javascript object notation (javascript 对象表示法) 是一种轻量级的数据交换语言,由javascript衍生而出,适用于.NET java c ...
- C# ASP.NET递归循环生成嵌套json结构树
1. 建立用来保存树结构数据的目标对象 public class TreeObject { public string name { get; set; } public string value { ...
- To Java程序员:切勿用普通for循环遍历LinkedList
ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ...
随机推荐
- Spring 配置自动扫描spring bean配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...
- java design
http://www.austintek.com/mvc/ http://msdn.microsoft.com/zh-cn/magazine/cc163419.aspx http://www.croc ...
- Delphi下的RTTI函数大全
http://ljz9425.blog.163.com/blog/static/369148572008111635253858/ Delphi下的RTTI(下) 2008-12-16 15:52:5 ...
- Git基本操作(Windows下)
在开始使用Git之前,我觉得是很有必要了解下Git与其他版本控制系统的差异与文件在Git中的三种状态.可以到下面这个网站看下:Git详解之一 Git起步,了解之后,可以对Git的基本操作有一个更清晰的 ...
- FFmpeg常用基本命令
FFmpeg常用基本命令 1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an output_file_video //分离视频流 ffmpeg -i inp ...
- fdisk磁盘分区
http://www.cr173.com/html/4336_1.html http://www.51cto.com/art/200602/20328.htm
- Android Studio导入Eclipse项目
随着Google 对新Android编辑器Android Studio(以下简称AS)的版本不断更新,越来越多的人开始由熟悉的编辑器Eclipse转向AS,而Eclipse开发团队也坦言将放弃对Ecl ...
- 高等数学(拉格朗日乘子法):NOI 2012 骑行川藏
[NOI2012] 骑行川藏 输入文件:bicycling.in 输出文件:bicycling.out 评测插件 时间限制:1 s 内存限制:128 MB NOI2012 Day1 Des ...
- 付出半个小时的笔误级BUG
一开始,我为了偷懒将所有的任务全都压在了一个浮动指针上: for (; CCPtr->S != NULL; CCPtr->S = CCPtr->S->next) // for ...
- Spring和Struct整合的三个方法
1.使用Spring 的 ActionSupport .2.使用Spring 的 DelegatingRequestProcessor 类.3.全权委托. 无论用那种方法来整合第一步就是要为strut ...