将 ADODB.RecordSet 输出为 JSON
Output ADODB.RecordSet as JSON
我正在尝试更改我的应用程序,以便它在发出 AJAX 请求时 输出 JSON 而不是 HTML一些数据。我有一个 ADODB 记录集。我需要逐行遍历它并 add/change/remove 不同的值。然后我需要将所有修改过的行 response.write
作为 JSON。我正在使用 JSON2.asp 所以我的应用程序已经支持 JSON.parse
& JSON.stringify
但我无法让它吐出多维数组 JSON.
set rs = conn.execute(strQuery)
if Not rs.EOF Then
rsArray = rs.GetRows() 'This pulls in all the results of the RecordSet as a 2-dimensional array
columnCount = ubound(rsArray,1)
rowCount = ubound(rsArray,2)
For rowIndex = 0 to rowCount 'Loop through rows as the outer loop
rsArray(3,0) = "somethingelse"
Next 'Move on to next row if there is one
response.write JSON.stringify(rsArray) & " _______ "
End If
我只需要能够遍历我的数据库查询结果,修改结果,然后将修改后的结果以JSON格式输出即可。正确的做法是什么?
尝试在 asp 页面顶部将内容类型设置为 "application/json"。
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Option Explicit
Response.Buffer=True
Response.ContentType="application/json"
Response.Charset="utf-8"
'' rest of your code.. your db operations
'' response write your json
%>
JSON2.asp
实现没有 "Load From Database" 函数,这意味着您必须自己实现一些东西来将 ADODB.Recordset
转换为 JSON 结构。
如果您愿意使用不同的脚本,可以使用 RCDMK on GitHub that does have a LoadRecordset()
method, it's called JSON object class 3.5.3 的实现。
这使得从 ADODB.Recordset
加载数据变得非常简单。
<!-- #include virtual="/jsonObject.class.asp" -->
<%
Response.LCID = 2057
'...
Dim rs: Set rs = conn.execute(strQuery)
Dim JSON: Set JSON = New JSONobject
Call JSON.LoadRecordset(rs)
Call Response.Clear()
Response.ContentType = "application/json"
Call JSON.Write()
%>
代码已使用断开连接的记录集进行测试,...
此处表示假定代码用于设置记录集、连接等
值得注意的是,您可以自己编写此代码,遍历 ADODB.Recordset
并构建 JSON 字符串并不是一个巨大的飞跃。但是,出于一些原因我会反对;
- 这是一项耗时的练习。
- 很容易遗漏一些东西(比如在生成输出时检查数字数据类型)。
- 根据它的编码方式,维护起来会很尴尬 (例如,如果不直接从记录集中注入 属性 名称,而是选择 "hardcode" 它们) .
- 为什么 reinvent the wheel?在野外有很多 public 实现来处理这里提出的问题。不可否认,有些比其他的要好,但需要五分钟的时间来包含它们并试一试。
为了完整起见,这里是我使用断开连接的记录集的本地测试代码
<!-- #include virtual="/jsonObject.class.asp" -->
<%
Call init()
Sub init()
Dim fields: fields = Array(Array("title", adVarChar, 50), Array("firstname", adVarChar, 50), Array("lastname", adVarChar, 50), Array("age", adInteger, 4))
Dim rs: Set rs = Server.CreateObject("ADODB.Recordset")
Call InsertRow(rs, fields, Array("Mr", "Joe", "Bloggs", 31))
Call InsertRow(rs, fields, Array("Mr", "John", "Smith", 42))
Response.LCID = 2057
Dim JSON: Set JSON = New JSONobject
Call JSON.LoadRecordset(rs)
Call Response.Clear()
Response.ContentType = "application/json"
Call JSON.Write()
End Sub
Sub InsertRow(ByVal rs, fields, values)
With rs
If rs.State <> adStateOpen Then
For Each fld In fields
Call .Fields.Append(fld(0), fld(1), fld(2))
Next
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
Call .Open()
End If
Call .AddNew()
For i = 0 To UBound(fields, 1)
.Fields(fields(i)(0)).Value = values(i)
Next
Call .Update()
Call .MoveFirst()
End With
End Sub
%>
输出:
{"data":[{"title":"Mr","firstname":"Joe","lastname":"Bloggs","age":31},{"title":"Mr","firstname":"John","lastname":"Smith","age":42}]}
给你。这对我有用。
set rs = conn.execute(strQuery)
c=0
Response.write "["
Do Until rs.eof
'Assign variables here with whatever you need to change
title = rs(0)
fName = rs(1)
lName = rs(2)
empID = rs(3)
With Response
if c > 0 then .write ", "
.write "{" & chr(34) & "Title" & chr(34) & " : " & chr(34) & title & chr(34) & ", " & chr(34) & "FirstName" & chr(34) & " : " & chr(34) & fName & chr(34) & ", "
.write chr(34) & "LastName" & chr(34) & " : " & chr(34) & lName & chr(34) & ", " & chr(34) & "EmpID" & chr(34) & " : " & chr(34) & empID & chr(34) & "}"
End With
c = c + 1
rs.MoveNext
Loop
Response.write "]"
这会将您的 JSON 对象直接写入页面。
我正在尝试更改我的应用程序,以便它在发出 AJAX 请求时 输出 JSON 而不是 HTML一些数据。我有一个 ADODB 记录集。我需要逐行遍历它并 add/change/remove 不同的值。然后我需要将所有修改过的行 response.write
作为 JSON。我正在使用 JSON2.asp 所以我的应用程序已经支持 JSON.parse
& JSON.stringify
但我无法让它吐出多维数组 JSON.
set rs = conn.execute(strQuery)
if Not rs.EOF Then
rsArray = rs.GetRows() 'This pulls in all the results of the RecordSet as a 2-dimensional array
columnCount = ubound(rsArray,1)
rowCount = ubound(rsArray,2)
For rowIndex = 0 to rowCount 'Loop through rows as the outer loop
rsArray(3,0) = "somethingelse"
Next 'Move on to next row if there is one
response.write JSON.stringify(rsArray) & " _______ "
End If
我只需要能够遍历我的数据库查询结果,修改结果,然后将修改后的结果以JSON格式输出即可。正确的做法是什么?
尝试在 asp 页面顶部将内容类型设置为 "application/json"。
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Option Explicit
Response.Buffer=True
Response.ContentType="application/json"
Response.Charset="utf-8"
'' rest of your code.. your db operations
'' response write your json
%>
JSON2.asp
实现没有 "Load From Database" 函数,这意味着您必须自己实现一些东西来将 ADODB.Recordset
转换为 JSON 结构。
如果您愿意使用不同的脚本,可以使用 RCDMK on GitHub that does have a LoadRecordset()
method, it's called JSON object class 3.5.3 的实现。
这使得从 ADODB.Recordset
加载数据变得非常简单。
<!-- #include virtual="/jsonObject.class.asp" -->
<%
Response.LCID = 2057
'...
Dim rs: Set rs = conn.execute(strQuery)
Dim JSON: Set JSON = New JSONobject
Call JSON.LoadRecordset(rs)
Call Response.Clear()
Response.ContentType = "application/json"
Call JSON.Write()
%>
代码已使用断开连接的记录集进行测试,...
此处表示假定代码用于设置记录集、连接等
值得注意的是,您可以自己编写此代码,遍历 ADODB.Recordset
并构建 JSON 字符串并不是一个巨大的飞跃。但是,出于一些原因我会反对;
- 这是一项耗时的练习。
- 很容易遗漏一些东西(比如在生成输出时检查数字数据类型)。
- 根据它的编码方式,维护起来会很尴尬 (例如,如果不直接从记录集中注入 属性 名称,而是选择 "hardcode" 它们) .
- 为什么 reinvent the wheel?在野外有很多 public 实现来处理这里提出的问题。不可否认,有些比其他的要好,但需要五分钟的时间来包含它们并试一试。
为了完整起见,这里是我使用断开连接的记录集的本地测试代码
<!-- #include virtual="/jsonObject.class.asp" -->
<%
Call init()
Sub init()
Dim fields: fields = Array(Array("title", adVarChar, 50), Array("firstname", adVarChar, 50), Array("lastname", adVarChar, 50), Array("age", adInteger, 4))
Dim rs: Set rs = Server.CreateObject("ADODB.Recordset")
Call InsertRow(rs, fields, Array("Mr", "Joe", "Bloggs", 31))
Call InsertRow(rs, fields, Array("Mr", "John", "Smith", 42))
Response.LCID = 2057
Dim JSON: Set JSON = New JSONobject
Call JSON.LoadRecordset(rs)
Call Response.Clear()
Response.ContentType = "application/json"
Call JSON.Write()
End Sub
Sub InsertRow(ByVal rs, fields, values)
With rs
If rs.State <> adStateOpen Then
For Each fld In fields
Call .Fields.Append(fld(0), fld(1), fld(2))
Next
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
Call .Open()
End If
Call .AddNew()
For i = 0 To UBound(fields, 1)
.Fields(fields(i)(0)).Value = values(i)
Next
Call .Update()
Call .MoveFirst()
End With
End Sub
%>
输出:
{"data":[{"title":"Mr","firstname":"Joe","lastname":"Bloggs","age":31},{"title":"Mr","firstname":"John","lastname":"Smith","age":42}]}
给你。这对我有用。
set rs = conn.execute(strQuery)
c=0
Response.write "["
Do Until rs.eof
'Assign variables here with whatever you need to change
title = rs(0)
fName = rs(1)
lName = rs(2)
empID = rs(3)
With Response
if c > 0 then .write ", "
.write "{" & chr(34) & "Title" & chr(34) & " : " & chr(34) & title & chr(34) & ", " & chr(34) & "FirstName" & chr(34) & " : " & chr(34) & fName & chr(34) & ", "
.write chr(34) & "LastName" & chr(34) & " : " & chr(34) & lName & chr(34) & ", " & chr(34) & "EmpID" & chr(34) & " : " & chr(34) & empID & chr(34) & "}"
End With
c = c + 1
rs.MoveNext
Loop
Response.write "]"
这会将您的 JSON 对象直接写入页面。