我正在尝试更改我的应用程序,以便在对某些数据发出AJAX请求时输出JSON而不是HTML 。我有一个ADODB RecordSet。我需要逐行循环并添加/更改/删除不同的值。然后我需要将所有修改过的行和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格式输出修改后的结果。什么是正确的方法?
答案 0 :(得分:3)
JSON2.asp
实现没有“从数据库加载”功能,这意味着您必须自己实现将ADODB.Recordset
转换为JSON结构的内容。
如果您愿意使用其他脚本,那么GitHub上的RCDMK实现了一个LoadRecordset()
方法的实现,它被称为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字符串并不是一个巨大的飞跃。但是,我会因为一些原因而反对;
为了完整起见,这是我使用断开记录集的本地测试代码
<!-- #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}]}
答案 1 :(得分:1)
你去吧。这对我有用。
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对象直接写入页面。
答案 2 :(得分:-1)
尝试将内容类型设置为&#34; application / json&#34;在你的asp页面上。
<%@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
%>