<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <!--#include file="aspjson1.19.asp" --> <% '商量,生成JSON格式 ' 创建一个空的字符串来存储结果 Dim jsonOutput jsonOutput = "" ' 连接到数据库 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) ' 开始构建JSON字符串 jsonOutput = "{""results"":[" ' 循环父类 Do While Not parentRs.EOF parentId = parentRs("id") parentName = parentRs("MingCheng") ' 构建父类部分 jsonOutput = jsonOutput & "{""id"":""" & parentId & """,""MingCheng"":""" & parentName & """,""children"":[" ' 子类查询 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) ' 循环子产品 Dim firstChild firstChild = True Do While Not childRs.EOF If Not firstChild Then jsonOutput = jsonOutput & "," End If childId = childRs("Comid") childTitle = childRs("Title") childOutUrl = childRs("OutUrl") childTJtitle = childRs("TJtitle") childLogo = childRs("Logo") childZSstar = childRs("ZSstar") ' 构建子类部分 jsonOutput = jsonOutput & "{""Comid"":""" & childId & """,""Title"":""" & childTitle & """,""OutUrl"":""" & childOutUrl & """,""TJtitle"":""" & childTJtitle & """,""Logo"":""" & childLogo & """,""ZSstar"":""" & childZSstar & """}" childRs.MoveNext firstChild = False Loop ' 完成父类部分 jsonOutput = jsonOutput & "]}" parentRs.MoveNext If Not parentRs.EOF Then jsonOutput = jsonOutput & "," End If Loop ' 完成JSON字符串 jsonOutput = jsonOutput & "]}" ' 关闭数据库连接 parentRs.Close Set parentRs = Nothing conn.Close Set conn = Nothing ' 输出JSON Response.ContentType = "application/json" Response.Write jsonOutput %>