asp将数组for循环输出json格式
ASP
2025-02-27 08:13
54
0
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
' 定义数据
dataArray = Array( _
CreateObject("Scripting.Dictionary"), _
CreateObject("Scripting.Dictionary"), _
CreateObject("Scripting.Dictionary") _
)
' 填充数据
dataArray(0).Add "id", 1
dataArray(0).Add "title", "北京"
dataArray(0).Add "caption", "首都"
dataArray(1).Add "id", 2
dataArray(1).Add "title", "合肥"
dataArray(1).Add "caption", "省会"
dataArray(2).Add "id", 3
dataArray(2).Add "title", "重庆"
dataArray(2).Add "caption", "直辖市"
' 输出JSON格式
Response.ContentType = "application/json"
Response.Write "["
For i = 0 To UBound(dataArray)
Response.Write "{"
Response.Write """id"": " & dataArray(i)("id") & ", "
Response.Write """title"": """ & dataArray(i)("title") & """, "
Response.Write """caption"": """ & dataArray(i)("caption") & """"
Response.Write "}"
If i < UBound(dataArray) Then
Response.Write ", "
End If
Next
Response.Write "]"
%>