asp根据数组赋值输出JSON格式
ASP
2025-02-26 14:36
41
0
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
' 定义原始数据
Dim data
data = Array( _
CreateObject("Scripting.Dictionary"), _
CreateObject("Scripting.Dictionary"), _
CreateObject("Scripting.Dictionary") _
)
data(0).Add "id", 1
data(0).Add "title", "北京"
data(0).Add "caption", "首都"
data(1).Add "id", 2
data(1).Add "title", "合肥"
data(1).Add "caption", "省会"
data(2).Add "id", 3
data(2).Add "title", "重庆"
data(2).Add "caption", "直辖市"
' 输出JSON格式
Response.ContentType = "application/json"
Response.Charset = "UTF-8"
Dim jsonString
jsonString = "["
Dim i
For i = 0 To UBound(data)
jsonString = jsonString & "{"
jsonString = jsonString & """id"": " & data(i)("id") & ","
jsonString = jsonString & """title"": """ & data(i)("title") & ""","
jsonString = jsonString & """caption"": """ & data(i)("caption") & """"
jsonString = jsonString & "}"
If i < UBound(data) Then
jsonString = jsonString & ","
End If
Next
jsonString = jsonString & "]"
Response.Write jsonString
%>