asp读取数据库关联的2个表之间的数据并以JSON格式输出

ASP   2025-02-27 17:52   48   0  

输出数组格式

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Response.ContentType = "text/html; charset=utf-8"%>
<%
'deepseek
' 定义数组来保存结果
Dim parentArray()
Dim childArray()

' 连接数据库
Dim conn, rsParent, rsChild
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=db;User ID=sa;Password=123456;"

' 查询父类数据
Set rsParent = Server.CreateObject("ADODB.Recordset")
rsParent.Open "select id, MingCheng from BDClass where ParentID='1' order by orders", conn,3,2

' 初始化父类数组
ReDim parentArray(rsParent.RecordCount - 1, 1)

' 循环父类数据
Dim i
i = 0
Do While Not rsParent.EOF
    parentArray(i, 0) = rsParent("id")
    parentArray(i, 1) = rsParent("MingCheng")
    
    ' 查询子类数据
    Set rsChild = Server.CreateObject("ADODB.Recordset")
    rsChild.Open "select C.Comid, C.Title, C.OutUrl, C.TJtitle, C.Logo, C.ZSstar from BDcompany T, Company C where T.Comid=C.Comid and C.IsPass=1 and T.classid='" & rsParent("id") & "' order by Orders, T.AddTime desc, T.ID desc", conn,3,2
    
    ' 初始化子类数组
    ReDim childArray(rsChild.RecordCount - 1, 5)
    
    ' 循环子类数据
    Dim j
    j = 0
    Do While Not rsChild.EOF
        childArray(j, 0) = rsChild("Comid")
        childArray(j, 1) = rsChild("Title")
        childArray(j, 2) = rsChild("OutUrl")
        childArray(j, 3) = rsChild("TJtitle")
        childArray(j, 4) = rsChild("Logo")
        childArray(j, 5) = rsChild("ZSstar")
        
        rsChild.MoveNext
        j = j + 1
    Loop
    
    ' 将子类数组保存到一个新的父类数组元素中
    ' 使用动态数组来存储子类数据
    ReDim Preserve parentArray(UBound(parentArray, 1), 2)
    parentArray(i, 2) = childArray
    
    rsParent.MoveNext
    i = i + 1
Loop

' 关闭记录集和连接
rsParent.Close
Set rsParent = Nothing
conn.Close
Set conn = Nothing

' 输出结果(示例)
For i = 0 To UBound(parentArray, 1)
    Response.Write "父类ID: " & parentArray(i, 0) & "<br>"
    Response.Write "父类名称: " & parentArray(i, 1) & "<br>"
    Dim childData
    childData = parentArray(i, 2)
    For j = 0 To UBound(childData, 1)
        Response.Write "子类ID: " & childData(j, 0) & "<br>"
        Response.Write "子类名称: " & childData(j, 1) & "<br>"
        ' 其他子类字段...
    Next
Next
%>


输出JSON格式

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Response.ContentType = "text/html; charset=utf-8"%>
<%
'deepseek,基于ceshi2输出JSON格式
' 定义变量
Dim jsonResult
jsonResult = "{""parentData"": ["

' 连接数据库
Dim conn, rsParent, rsChild
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=db;User ID=sa;Password=123456;"

' 查询父类数据
Set rsParent = Server.CreateObject("ADODB.Recordset")
rsParent.Open "select id, MingCheng from BDClass where ParentID='1' order by orders", conn

' 循环父类数据
Dim isFirstParent
isFirstParent = True
Do While Not rsParent.EOF
    If Not isFirstParent Then
        jsonResult = jsonResult & ","
    End If
    isFirstParent = False
    
    jsonResult = jsonResult & "{""id"": """ & rsParent("id") & """, ""MingCheng"": """ & rsParent("MingCheng") & """, ""childData"": ["
    
    ' 查询子类数据
    Set rsChild = Server.CreateObject("ADODB.Recordset")
    rsChild.Open "select C.Comid, C.Title, C.OutUrl, C.TJtitle, C.Logo, C.ZSstar from BDcompany T, Company C where T.Comid=C.Comid and C.IsPass=1 and T.classid='" & rsParent("id") & "' order by Orders, T.AddTime desc, T.ID desc", conn
    
    ' 循环子类数据
    Dim isFirstChild
    isFirstChild = True
    Do While Not rsChild.EOF
        If Not isFirstChild Then
            jsonResult = jsonResult & ","
        End If
        isFirstChild = False
        
        jsonResult = jsonResult & "{"
        jsonResult = jsonResult & """Comid"": """ & rsChild("Comid") & """, "
        jsonResult = jsonResult & """Title"": """ & rsChild("Title") & """, "
        jsonResult = jsonResult & """OutUrl"": """ & rsChild("OutUrl") & """, "
        jsonResult = jsonResult & """TJtitle"": """ & rsChild("TJtitle") & """, "
        jsonResult = jsonResult & """Logo"": """ & rsChild("Logo") & """, "
        jsonResult = jsonResult & """ZSstar"": """ & rsChild("ZSstar") & """"
        jsonResult = jsonResult & "}"
        
        rsChild.MoveNext
    Loop
    
    jsonResult = jsonResult & "]}"
    
    rsParent.MoveNext
Loop

jsonResult = jsonResult & "]}"

' 关闭记录集和连接
rsParent.Close
Set rsParent = Nothing
conn.Close
Set conn = Nothing

' 设置响应头为JSON格式
Response.ContentType = "application/json"
Response.Write jsonResult
%>



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