`
xitong
  • 浏览: 6158126 次
文章分类
社区版块
存档分类
最新评论

Asp.Net 使用“递归算法”生成目录树的JSON格式(值得收藏)

 
阅读更多

声明本文摘自:http://www.cnblogs.com/yangrixing/archive/2012/08/23/2651900.html

今天在群里有群友求助,吾正好有空,顺便解答一下。

废话少说中,直切主题。

主要解决内容:将一个数据表生成一个JSON格式的,有层次结构的“目录”树。

生成的JSON字符串经过格式化后,如下图所示:


图1 效果图

如何实现呢?

一、给出数据库的结构,让大家思考一下。

数据库的结构和数据截图分别如下:

图2 数据库结构 图3 数据库中的数据

好了,数据库的结构已经给出了,那,应该如何生成像图1所示的JSON数据格式呢?

毫无疑问,必须用到“序列化”吧?

毫无疑问,必须用到“递归算法”吧?如果您不知道的除外~我的博客里有收集了一些“递归算法”的经典应用,欢迎移步。

嗯,没错。那就让我们来看看如何设计这个递归算法了。

第一步:根所JSON格式,先写相应的实体类,为读取数据后封装提供支持。

先来分析一下JSON数据的结构,终合而言,是由一个text字段,children[]数据组成的对象(我们且称之为item对象吧),而children[]数组中,又包含了该对象。因为,可以利用此特性,设计一个类以支持递归算法的实现。

废话少说了,直接贴码了:

代码1 封装好的类,如下图所示

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Data;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Data;
usingSystem.Data.SqlClient;
/// <summary>
/// 节点的实体类,记录了数据库中的3个字段
/// 为的是方便操作
/// </summary>
[Serializable]
publicclassItems
{
publicintbh;
publicstringtext;
publicintparentBh;
}
/// <summary>
/// 节点类,基础类
/// </summary>
[Serializable]
publicclassJieDian
{
publicstringtext="";
publicjieDian[] children = null;
}
[Serializable]
publicclassCliet
{
//根据parentBh获取相应的子目录集合
publicItems[] GetTheItems (stringparentBh)
{
//根据父节点获取选项集合
stringsql = "select * from Table_3 where parentBh="+parentBh; //这里改成你的数据库表
SqlDataReader dr = SQLHelp.ExecuteReader(sql); //这里我直接调用了我库中的类
List<Items> items = newSystem.Collections.Generic.List<Items>();
while(dr.Read())
{
Items i = newitems();
i.bh = dr.GetInt32(0);
i.text = dr.GetString(1);
i.parentBh = dr.GetInt32(2);
items.Add(i);
}
dr.Close(); //一定要对这进行关闭
returnitems.ToArray(); //返回
}
//生成树的方法
publicvoidcreatTheTree ( stringparentBh ,jieDian jd)
{
//获取
Items[] items = GetTheItems(parentBh);
if(items.Length == 0)
return; //如果没有字节点了,那就返回空
List<JieDian> jdList = newSystem.Collections.Generic.List<JieDian>();
for(inti = 0; i < items.Length; i++)
{
JieDian jiedian = newJieDian();
jiedian.text = items[i].text;
creatTheTree(items[i].bh.ToString(), jiedian);
jdList.Add(jiedian);
}
jd.children = jdList.ToArray(); //由于对象是引用类型,因为可以改变参数的值
}
}

第二步:调用写好的类,序列化成JSON格式

新建一个“一般处理程序(.ashx),直接用C#的一般处理程序生成,以便前台通过异步接收到生成的字符串。

代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@ WebHandler Language="C#"Class="Handler"%>
usingSystem;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Text;
usingSystem.IO;
usingSystem.Runtime.Serialization.Json;
usingDkl.New3.Chart;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Reflection;
publicclassHandler : IHttpHandler {
publicvoidProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
Cliet clite = newCliet();
JieDian root = newJieDian();
root.text = "根节点";
clite.creatTheTree("0", root); //根节点的parentBh值为"0"
//对root对象进行序列化
DataContractJsonSerializer dc = newDataContractJsonSerializer(root.GetType());
MemoryStream ms = newMemoryStream();
dc.WriteObject(ms, root);
byte[] aryByte = ms.ToArray();
stringjson = Encoding.UTF8.GetString(aryByte, 0, aryByte.Length);
ms.Close();
context.Response.Write(json.ToString());
}
publicboolIsReusable {
get{
returnfalse;
}
}
}
?
1

第三步:运行

运行结果如下:

image

图4 运行结果

经JSON数据格式化后,工具地址(http://www.bejson.com/go.html?u=http://www.bejson.com/jsonview2/)

得到以下视图:


图5 格式化

第四步:反思。

主要有以下几点反思

1、程序还有诸多一足,只能是一个Deom,还有很多安全方面、序列化顺序、序列化速度、序列化的名称等可以继续完善。

2、加深了对递归算法的理解。

3、这样的设计是否得当?是不是还有更好的办法?

4、这样的设计,容易扩展吗?

嗯,相信日后会继续慢慢完善的。

亲爱的,今天是七夕喔,如果觉得本文对你有用,欢迎点击右下方的“收藏”喔~也可以关注我呢!

欢迎各位博园给我提意见!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics