2

我正在尝试将 ASP 中的 SQL 查询序列化为这样的 json 对象:

[["name1","phone1","email1","address1"],["name2","phone2","email2","address2"]]

我做了两个循环来遍历数据库的列和行,这就是我到目前为止的方式;

<!--#include file="JSON_2.0.4.asp"-->


SQL = "SELECT name, phone, email, address FROM clients"
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")
Connection.Open "Provider=sqloledb;SERVER=localhost;DATABASE=mydatabase;UID=;PWD=1234;"  
Recordset.Open SQL,Connection

rsArray = Recordset.getRows()       

ReDim arrObj(UBound(rsArray, 2) - 1)

Dim i, j

For i = 1 To UBound(rsArray, 2)

    ReDim arrProp(UBound(rsArray, 1))

    For j = 0 To UBound(rsArray, 1)
        arrProp(j) = rsArray(j, i)
    Next 

Next

Response.Write toJSON(arrProp)

我在这里做错了什么,我试图改变每一个可能的值。结果只给了我表格的最后一行(如下所示),当然我想从表格中检索每一行。提前感谢您提供任何可能的答案。

[“La Musical Fashion”,“214478457”,“musical@fashion.pt”,“Av. Quinta Grande,8 R/C Esq.”]

4

1 回答 1

2

需要建立一个Array of ArrayslikeArray(Array(col1,col2,col3), Array(col1,col2,col3))才能通过获得想要的结果toJSON
你可以用下面的方法来做到这一点。

rsArray = Recordset.getRows()

ReDim Rows(UBound(rsArray, 2))
ReDim Row(UBound(rsArray, 1))
For i = 0 To UBound(Rows)
    For j = 0 To UBound(Row)
        Row(j) = rsArray(j, i)
    Next 
    Rows(i) = Row
Next
Response.Write toJSON(Rows)
于 2012-09-01T18:11:25.347 回答