ASP返回JQuery EasyUI树形要求的JSON格式

根据http://blog.csdn.net/apollokk/article/details/41382479中提供的数据将结果格式化为JQuery EasyUI树形要求的JSON格式

<%
Response.Charset="utf-8"
Session.CodePage=65001

Set conn = Server.CreateObject("ADODB.Connection") 
connstr="Driver={SQL Server};server=.\sqlexpress;uid=sa;pwd=sa;database=dgmloa;"
conn.Open connstr 

set rs = conn.execute("exec [pGetDepartment]")
json = ""
if not rs.eof then
	json = "["
	'第一条数据
	json = json & "{""id"":" & rs("id") & ",""text"":""" & Replace(rs("department"),"""","\""") & """"
	'当前层级
	level = rs("level")
	rs.movenext
	while not rs.eof
		if level = rs("level") then						'如果是同级则闭合
			json = json & "},"
		elseif level > rs("level") then					'如果遇到上一级则根据级别之差进行闭合
			json = json & "}"
			for i = 1 to (level - rs("level"))
				json = json & "]}"
			next
			json = json & ","
		else											'如果遇到下级则添加前缀
			json = json & ",""children"":["
		end if
		'输出本条数据
		json = json & "{""id"":" & rs("id") & ",""text"":""" & Replace(rs("department"),"""","\""") & """"
		level = rs("level")
		rs.movenext	
	wend
	'最后的闭合
	for i = 1 to level
             json = json & "}]"
        next
	Response.Write(json)
end if

if rs.state=1 then rs.close
if conn.state=1 then conn.close
set rs=nothing
set conn=nothing
%>


posted @ 2014-11-22 15:59  网事  阅读(293)  评论(0编辑  收藏  举报