asp使用数组输出JSON格式
ASP
2025-02-26 14:13
62
0
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
' 定义数据
Dim data
data = Array( _
Array(1, "北京", "首都"), _
Array(2, "合肥", "省会"), _
Array(3, "重庆", "直辖市") _
)
' 初始化JSON字符串
Dim jsonString
jsonString = "["
' 循环遍历数据
Dim i
For i = LBound(data) To UBound(data)
' 拼接JSON格式字符串
jsonString = jsonString & "{"
jsonString = jsonString & """id"" : """ & data(i)(0) & ""","
jsonString = jsonString & """title"" : """ & data(i)(1) & ""","
jsonString = jsonString & """caption"" : """ & data(i)(2) & """"
jsonString = jsonString & "}"
' 如果不是最后一个元素,添加逗号
If i < UBound(data) Then
jsonString = jsonString & ","
End If
Next
' 结束JSON字符串
jsonString = jsonString & "]"
' 输出JSON字符串
Response.ContentType = "application/json"
Response.Write jsonString
%>