<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <!--#include file="aspJSON1.19.asp" --> <% ' 定义数组来保存结果 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, 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 ' 创建aspJSON类的实例 Dim json Set json = New aspJSON ' 遍历父类数组 For i = 0 To UBound(parentArray, 1) ' 创建一个父类集合 Dim parentCollection Set parentCollection = json.Collection() ' 添加父类的ID和名称 parentCollection.Add "id", parentArray(i, 0) parentCollection.Add "MingCheng", parentArray(i, 1) ' 创建一个子类集合 Dim childCollection Set childCollection = json.Collection() ' 遍历子类数组 For j = 0 To UBound(parentArray(i, 2), 1) ' 创建一个子类项集合 Dim childItemCollection Set childItemCollection = json.Collection() ' 添加子类的各项数据 childItemCollection.Add "Comid", parentArray(i, 2)(j, 0) childItemCollection.Add "Title", parentArray(i, 2)(j, 1) childItemCollection.Add "OutUrl", parentArray(i, 2)(j, 2) childItemCollection.Add "TJtitle", parentArray(i, 2)(j, 3) childItemCollection.Add "Logo", parentArray(i, 2)(j, 4) childItemCollection.Add "ZSstar", parentArray(i, 2)(j, 5) ' 将子类项集合添加到子类集合中 childCollection.Add j, childItemCollection Next ' 将子类集合添加到父类集合中 parentCollection.Add "children", childCollection ' 将父类集合添加到json对象的数据中 json.data.Add i, parentCollection Next ' 设置响应头为JSON格式 Response.ContentType = "application/json" ' 输出JSON数据 Response.Write json.JSONoutput() %>