<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <%Response.ContentType = "text/html; charset=utf-8"%> <!--#include file="aspjson1.19.asp" --> <% '商量 ' 首先,需要包含一个JSON库来生成JSON格式的输出 ' 这里假设你已经有了一个可以使用的JSON库,例如aspJSON库 ' 你可以从 https://github.com/MarcusNebel/aspJSON 下载并包含到你的项目中 ' 创建一个空的数组来存储结果 Dim results Set results = Server.CreateObject("Scripting.Dictionary") ' 连接到数据库 Dim conn Set conn = Server.CreateObject("ADODB.Connection") conn.Open "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=db;User ID=sa;Password=123456;" ' 替换为你的数据库连接字符串 ' 父类查询 Dim parentId, parentName, parentRs, parentSql parentSql = "SELECT id, MingCheng FROM BDClass WHERE ParentID='1' ORDER BY orders" Set parentRs = conn.Execute(parentSql) ' 循环父类 Do While Not parentRs.EOF parentId = parentRs("id") parentName = parentRs("MingCheng") ' 创建一个字典来存储当前父类及其子产品 Dim parentDict Set parentDict = Server.CreateObject("Scripting.Dictionary") parentDict.Add "id", parentId parentDict.Add "MingCheng", parentName parentDict.Add "children", Server.CreateObject("Scripting.Dictionary") ' 子类查询 Dim childRs, childSql, childId, childTitle, childOutUrl, childTJtitle, childLogo, childZSstar childSql = "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='" & parentId & "' ORDER BY Orders, T.AddTime DESC, T.ID DESC" Set childRs = conn.Execute(childSql) ' 循环子产品 Do While Not childRs.EOF childId = childRs("Comid") childTitle = childRs("Title") childOutUrl = childRs("OutUrl") childTJtitle = childRs("TJtitle") childLogo = childRs("Logo") childZSstar = childRs("ZSstar") ' 创建一个字典来存储当前子产品 Dim childDict Set childDict = Server.CreateObject("Scripting.Dictionary") childDict.Add "Comid", childId childDict.Add "Title", childTitle childDict.Add "OutUrl", childOutUrl childDict.Add "TJtitle", childTJtitle childDict.Add "Logo", childLogo childDict.Add "ZSstar", childZSstar ' 添加到父类的children字典中 parentDict("children").Add childId, childDict childRs.MoveNext Loop ' 添加到结果字典中 results.Add parentId, parentDict parentRs.MoveNext Loop ' 关闭数据库连接 parentRs.Close Set parentRs = Nothing conn.Close Set conn = Nothing %> <% ' 假设results字典已经按照之前的代码填充完成 Dim key, value, childKey, childValue ' 遍历results字典 For Each key In results.Keys ' 获取父类信息 Dim parentDict2 Set parentDict2 = results(key) Dim parentId2, parentName2, children2 parentId2 = parentDict2("id") parentName2 = parentDict2("MingCheng") Set children2 = parentDict2("children") ' 输出父类信息 Response.Write "<h2>父类ID: " & parentId2 & " - 父类名称: " & parentName2 & "</h2>" Response.Write "<ul>" ' 遍历子产品字典 For Each childKey In children2.Keys Dim childDict2 Set childDict2 = children2(childKey) Dim childId2, childTitle2, childOutUrl2, childTJtitle2, childLogo2, childZSstar2 childId2 = childDict2("Comid") childTitle2 = childDict2("Title") childOutUrl2 = childDict2("OutUrl") childTJtitle2 = childDict2("TJtitle") childLogo2 = childDict2("Logo") childZSstar2 = childDict2("ZSstar") ' 输出子产品信息 Response.Write "<li>" Response.Write "子产品ID: " & childId2 & "<br>" Response.Write "标题: " & childTitle2 & "<br>" Response.Write "外链: " & childOutUrl2 & "<br>" Response.Write "推荐标题: " & childTJtitle2 & "<br>" Response.Write "Logo: " & childLogo2 & "<br>" Response.Write "星级: " & childZSstar2 & "<br>" Response.Write "</li>" Next Response.Write "</ul>" Next %>