嵌入式JSON与aspjson

时间:2015-11-21 00:58:28

标签: json asp-classic

我想以

形式在ASP AJAX响应中生成一些JSON
{
  "Result":"OK",
  "Records":[
    {"Id":1,"Name":"Ben"},
    {"Id":2,"Name":"Doug"},
    {"Id":3,"Name":"Isaac"}
  ]
}

我正在使用aspjson并获得"记录"从我的数据库使用

<%@Language="VBScript" %>
<!-- #INCLUDE virtual="/Includes/db_connection_inc.asp" -->
<!-- #INCLUDE virtual="/jquery/JSON.asp" -->
<!-- #INCLUDE virtual="/jquery/JSON_util.asp" -->
<%
    Dim cnn, crs, oRSeof
    tablename = "[myDB].[dbo].[Driver]"
    Set cnn = Createobject("ADODB.Connection")
    cnn.Open DB_CONNECTION
    Set crs=CreateObject("ADODB.recordset")
    sqlQuery = "SELECT drvID, DriverName FROM Driver"
    rsJSON = CStr(QueryToJSON(cnn, sqlQuery).Flush)
    Response.Write rsJSON

    Set crs = Nothing
    Set cnn = Nothing
%>

将结果作为记录集/结构嵌入外部对象的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我找到了tugrulto对https://code.google.com/p/aspjson/wiki/SQLtoJSON的评论的解决方案。

<%@Language="VBScript" %>
<!-- #INCLUDE virtual="/Includes/db_connection_inc.asp" -->
<!-- #INCLUDE virtual="/jquery/JSON.asp" -->
<!-- #INCLUDE virtual="/jquery/JSON_util.asp" -->
<%
    '{
    ' "Result":"OK",
    ' "Records":[
    '  {"drvID":1,"Driver":"Ben"},
    '  {"drvID":2,"Driver":"Isaac"}
    ' ]
    '}

    tablename = "[readymix].[dbo].[Driver]"
    Set cnn = Createobject("ADODB.Connection")
    cnn.Open DB_CONNECTION
    Set crs=CreateObject("ADODB.recordset")
    sqlQuery = "SELECT drvID, Driver FROM " & tablename

    set jsout = jsObject()
    jsout("Result") = "OK"
    Set jsout("Records") = QueryToJSON(cnn, sqlQuery)   '<-- The important bit.
    jsout.Flush

    Set crs = Nothing
    Set cnn = Nothing
%>