根据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=xxxx;"
conn.Open connstr
set rs = conn.execute("exec [pGetDepartment]")
json = ""
inCount = 0 '进多少
outCount = 0 '退多少
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 & "]}"
outCount = outCount + 1
next
json = json & ","
else '如果遇到下级则添加前缀
json = json & ",""children"":["
inCount = inCount + 1
end if
'输出本条数据
json = json & "{""id"":" & rs("id") & ",""text"":""" & Replace(rs("department"),"""","\""") & """"
level = rs("level")
rs.movenext
wend
'最后的闭合
for i= outCount to inCount
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
%>