arrayToJSON

by lneely on February 8th, 2010

this is a speed-optimized version of RStoJSON by Tracy Dryden
http://www.tek-tips.com/faqs.cfm?fid=6410

instead of an ADO recordset, it uses an array created by GetRows(). the column names can be built by looping through the Fields collection.

Syntax: No syntax
Show lines - Hide lines - Show in textbox - Download
 
function arrayToJSON(arr, colNames, varName)
	dim iRowLoop, iColLoop
	dim sFld, sFlds, sRec, sRecs, sRecordSet
 
	if ubound(arr) = 0 then 
		arrayToJSON = "var " & varName & " = { rows: [ ] }"		' EOF
	else
		for iRowLoop = 0 to ubound(arr, 2)  ' for each row (do while not rs.eof)...
			sFlds = ""
			for iColLoop = 0 to ubound(arr, 1)	' for each field...
				sFld = """" & colNames(iColLoop) & """:""" & toUnicode(arr(iColLoop, iRowLoop) & "") & """"
				sFlds = sFlds & iif(sFlds <> "", ",", "") & sFld & vbCrLf & vbTab & vbTab
			next 'field
			sRec = vbTab & vbTab & "{" & sFlds & "}"
			sRecs = sRecs & iif(sRecs <> "", "," & vbCrLf, "") & sRec
		next 'row
		sRecordSet = "var " & varName & " = { " & vbCrLf & vbTab & "rows: [" & vbCrLf & sRecs & vbCrLf & vbTab & "]" & vbCrLf
		sRecordSet = sRecordSet & "}"
		arrayToJSON = sRecordSet
	end if
end function
 

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS