I'm trying to change my application so that it outputs JSON instead of HTML when it makes an AJAX request for some data. I have an ADODB RecordSet. I need to loop through it row-by-row and add/change/remove different values. Then I need to take all the modified rows and response.write
them as JSON. I'm using JSON2.asp so my application already supports JSON.parse
& JSON.stringify
but I can't get it to spit out the multi-dimensional array as 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
I just need to be able to go through the results of my database query, modify the results, and then output the modified results in JSON format. What's the right way to do this?
The
JSON2.asp
implementation doesn't have a "Load From Database" function which means you will have to implement something to convert theADODB.Recordset
to a JSON structure yourself.If you are willing to use a different script there is an implementation by RCDMK on GitHub that does have a
LoadRecordset()
method, it's called JSON object class 3.5.3.This makes loading data from an
ADODB.Recordset
really straightforward.Code has been tested using a disconnected recordset, the
...
here denote assumed code to setup your recordset, connection etcIt's worth noting you could write this yourself, it's not a huge leap to loop through an
ADODB.Recordset
and build a JSON string. However, I would argue against for a few reasons;Just for completeness here is my local test code using a disconnected recordset
Output:
try setting content-type to "application/json" on top of your asp page.
Here ya go. This works for me.
This will write your JSON object directly to the page.