asp将数组for循环输出json格式

ASP   2025-02-26 20:16   49   0  
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>

<%
Response.ContentType = "application/json"
Response.Charset = "UTF-8"

' 定义数据
data = Array( _
    CreateDictionary(1, "北京", "首都"), _
    CreateDictionary(2, "合肥", "省会"), _
    CreateDictionary(3, "重庆", "直辖市") _
)

' 创建JSON字符串
Dim jsonOutput
jsonOutput = "["

For i = LBound(data) To UBound(data)
    jsonOutput = jsonOutput & "{"
    jsonOutput = jsonOutput & """id"": " & data(i)("id") & ","
    jsonOutput = jsonOutput & """title"": """ & data(i)("title") & ""","
    jsonOutput = jsonOutput & """caption"": """ & data(i)("caption") & """"
    jsonOutput = jsonOutput & "}"
    If i < UBound(data) Then
        jsonOutput = jsonOutput & ","
    End If
Next

jsonOutput = jsonOutput & "]"

' 输出JSON字符串
Response.Write(jsonOutput)

' 辅助函数:创建字典对象
Function CreateDictionary(id, title, caption)
    Dim dict
    Set dict = Server.CreateObject("Scripting.Dictionary")
    dict.Add "id", id
    dict.Add "title", title
    dict.Add "caption", caption
    Set CreateDictionary = dict
End Function
%>


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