<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <!--#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 Dim keysArray keysArray = results.Keys ' 遍历键数组 For i = LBound(keysArray) To UBound(keysArray) Dim currentKey, currentValue currentKey = keysArray(i) 'Response.Write "键: " & currentKey Next ' 释放对象 ' 使用aspJSON库将结果字典转换为JSON格式 'Dim oJSON 'Set oJSON = New aspJSON 'With oJSON.data 'for b=0 to results.count 'dim hang 'hang = results(b) '.Add n, oJSON.Collection() 'With .item(n) '.Add "id", ""&results(b)("id")&"" 'End With 'n=n+1 'next 'End With 'Response.Write oJSON.JSONoutput() ' 输出JSON 'Response.ContentType = "application/json" 'Response.Write oJSON.ToString() 'Set results = Nothing ' 转换字典为JSON格式输出 Response.ContentType = "application/json" Dim resultJSON resultJSON = "{" Dim key, parentItem For Each key In results.Keys Set parentItem = results.Item(key) ' 开始父类信息输出 resultJSON = resultJSON & """key"":{" resultJSON = resultJSON & """id"":""" & parentItem("id") & """," resultJSON = resultJSON & """MingCheng"":""" & parentItem("MingCheng") & """," resultJSON = resultJSON & """children"":{" ' 处理子产品数据 Dim childKey, childItem For Each childKey In parentItem("children").Keys Set childItem = parentItem("children").Item(childKey) ' 子类信息输出 resultJSON = resultJSON & """" & childKey & """" & ":{" resultJSON = resultJSON & """Comid"":""" & childItem("Comid") & """," resultJSON = resultJSON & """Title"":""" & childItem("Title") & """," resultJSON = resultJSON & """OutUrl"":""" & childItem("OutUrl") & """," resultJSON = resultJSON & """TJtitle"":""" & childItem("TJtitle") & """," resultJSON = resultJSON & """Logo"":""" & childItem("Logo") & """," resultJSON = resultJSON & """ZSstar"":""" & childItem("ZSstar") & """" resultJSON = resultJSON & "}," Next ' 移除最后一个多余的逗号 If Mid(resultJSON, Len(resultJSON), 1) = "," Then resultJSON = Left(resultJSON, Len(resultJSON) - 1) End If ' 闭合子项和父项 resultJSON = resultJSON & "}" resultJSON = resultJSON & "}," Next ' 移除最后一个多余的逗号并闭合JSON对象 If Mid(resultJSON, Len(resultJSON), 1) = "," Then resultJSON = Left(resultJSON, Len(resultJSON) - 1) End If resultJSON = resultJSON & "}" ' 输出JSON字符串 Response.Write resultJSON %>