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

ASP   2025-03-01 16:20   53   0  
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Response.ContentType = "text/html; charset=utf-8"%>
<!--#include  file="aspJSON1.19.asp" -->
<%
'deepseek
' 定义数组来保存结果
Dim parentArray()
Dim childArray()

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

' 查询父类数据
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, 2)
' 循环父类数据
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
    
    ' 将子类数组保存到父类数组中
    parentArray(i, 2) = childArray
    rsParent.MoveNext
    i = i + 1
Loop

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

' 创建 aspJSON 对象
Set oJSON = New aspJSON

' 将 parentArray 数据转换为 JSON 格式
Dim parent
For i = LBound(parentArray, 1) To UBound(parentArray, 1)
    Dim parentID, parentName, children
    parentID = parentArray(i, 0)
    parentName = parentArray(i, 1)
    children = parentArray(i, 2)
    
    ' 添加父类数据
    oJSON.data.Add parentID, oJSON.Collection()
    oJSON.data(parentID).Add "MingCheng", parentName
    
    ' 初始化子类集合
    Dim childrenCollection
    Set childrenCollection = oJSON.Collection()
    
    ' 添加子类数据到集合中
    Dim child
    For j = LBound(children, 1) To UBound(children, 1)
        Dim childCollection
        Set childCollection = oJSON.Collection()
        childCollection.Add "Comid", children(j, 0)
        childCollection.Add "Title", children(j, 1)
        childCollection.Add "OutUrl", children(j, 2)
        childCollection.Add "TJtitle", children(j, 3)
        childCollection.Add "Logo", children(j, 4)
        childCollection.Add "ZSstar", children(j, 5)
        
        ' 将子类集合添加到父类的子类集合中
        childrenCollection.Add childrenCollection.Count, childCollection
    Next
    
    ' 将子类集合添加到父类数据中
    oJSON.data(parentID).Add "children", childrenCollection
Next

' 输出 JSON 数据
Response.Write oJSON.JSONoutput()
%>


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