asp将数据库读取的数据输出json格式

ASP   2025-02-26 16:20   63   0  
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Response.ContentType = "application/json"
 
Dim conn, rs 
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "数据库连接字符串"
 
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT id, title, caption FROM cities", conn 
 
Dim jsonStr 
jsonStr = "["
 
Do While Not rs.EOF 
    jsonStr = jsonStr & "{"
    jsonStr = jsonStr & """id"":" & rs("id") & ","
    jsonStr = jsonStr & """title"":""" & Server.HTMLEncode(rs("title")) & ""","
    jsonStr = jsonStr & """caption"":""" & Server.HTMLEncode(rs("caption")) & """"
    jsonStr = jsonStr & "}"
    
    rs.MoveNext 
    If Not rs.EOF Then jsonStr = jsonStr & ","
Loop 
 
jsonStr = jsonStr & "]"
 
Response.Write jsonStr 
 
rs.Close 
Set rs = Nothing 
conn.Close 
Set conn = Nothing 
%>


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。