- ·上一篇文章:如何安装asp运行环境(IIS安装)?
- ·下一篇文章:asp中使用SVG <path>来画任意图形
asp生成json的实例--asp读取表中内容生成json
假设表名category
字段名 cat_id,cat_name,parent_id(父ID )
使用asp读取category表中的数据生成json。
对asp来说,json就是字符串,在asp中没有直接将数组转成json的函数,所以要生成json就需要自己读取表然后拼成json。
<
%
'原创文章,来源www.K88.NET,转载请注明来源
'连接数据库代码
DB="1.mdb"
path=Server.MapPath(DB)
set conn=server.createobject("adodb.Connection")
connstr="provider=Microsoft.Jet.OLEDB.4.0
Data Source="&
path
conn.Open connstr
'asp生成json输出结果
Response.write Json()
Function Json()
Str = "
{""list"":["
Str = Str &
TableToJson(0) &
","
If Len(Str)>
11 Then
Str = Left(Str,Len(Str)-1)
End If
Str = Str &
"]}"
Json = Str
End Function
'读取数据库生成json函数
function TableToJson(parentID)  
 
 
Dim m,n
m = ""
sql = "select cat_id,cat_name from category where parent_id = "&
parentID
 
 
Set rs = conn.execute(sql)  
if not rs.eof Then  
 
n=""
 
Do While Not rs.eof
 
 
 
cat_id = rs("cat_id")
 
cat_name = rs("cat_name")
 
n = n &
"
{"
 
n = n &
"""cat_id"":"&
cat_id&
","
 
n = n &
"""cat_name"":"""&
cat_name&
""","
 
n = n &
"""childList"": [" &
TableToJson(cat_id)&
"]"
 
n = n &
"},
"
 
rs.movenext
 
Loop
 
If Len(n)>
0 Then
 
 
n = Left(n,Len(n)-1)
 
End If
 
m = m &
n
end if
rs.close
Set rs = nothing
TableToJson = m
end function  
%>
asp生成json的实例--asp读取表中内容生成json