asp读取数据库数据输出JSON格式
ASP
2025-02-27 08:11
56
0
<%
Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=bds123_db;User ID=sa;Password=sa;CharacterSet=UTF8"
Dim rs, dict, jsonResult
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT top 10 * FROM News", conn ' 假设conn已定义好连接
Do While Not rs.EOF
Set dict = CreateObject("Scripting.Dictionary")
For Each fld In rs.Fields
If Not dict.Exists(fld.Name) Then
dict.Add fld.Name, fld.Value
End If
Next
' 手动组装单条记录对应的JSON部分
Dim itemJson
itemJson = "{"
For Each key In dict.Keys
itemJson = itemJson & """" & key & """ : """ & dict(key) & """, "
Next
itemJson = Left(itemJson, Len(itemJson)-2) & "}"
response.Write itemJson & ", "
rs.MoveNext
Loop
' 清理资源
rs.Close
Set rs = Nothing
%>