色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

ASP獲取數據庫json數據

洪振霞1年前5瀏覽0評論

ASP是一種用于Web服務器上運行動態網站的技術。為了讓Web網站更加豐富動態,我們通常需要從數據庫中獲取數據。而JSON是一種輕量級的數據交換格式,在Web開發中廣泛應用。本文將介紹如何在ASP環境下獲取數據庫中的JSON數據。

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("test.mdb")
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM test", conn, adOpenKeyset, adLockOptimistic
Dim jsonArr
jsonArr = array()
While Not rs.EOF
set item = CreateObject("Scripting.Dictionary")
item.Add "id", rs.Fields("id").Value
item.Add "name", rs.Fields("name").Value
jsonArr = AppendToJSONArray(jsonArr, item)
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
Dim jsonStr
jsonStr = ConvertToJson(jsonArr)
Response.ContentType = "application/json"
Response.Write(jsonStr)
Function AppendToJSONArray(jsonArr1, item)
Dim i
i = UBound(jsonArr1) + 1
ReDim Preserve jsonArr1(i)
Set jsonArr1(i) = item
AppendToJSONArray = jsonArr1
End Function
Function ConvertToJson(obj)
Dim jsonStr, i    
jsonStr = ""
If IsObject(obj) Then
If IsArray(obj) Then
jsonStr = jsonStr & "["
For i = 0 To UBound(obj) - 1
jsonStr = jsonStr & ConvertToJson(obj(i)) & ","
Next
jsonStr = jsonStr & ConvertToJson(obj(i)) & "]"
Else
Set keys = obj.Keys
jsonStr = jsonStr & "{"
For Each key In keys
jsonStr = jsonStr & """" & key & """:"
jsonStr = jsonStr & ConvertToJson(obj.Item(key)) & ","
Next
jsonStr = Left(jsonStr, Len(jsonStr) - 1) & "}"
End If
Else
jsonStr = jsonStr & """" & obj & """"
End If
ConvertToJson = jsonStr
End Function

以上代碼中,首先連接數據庫,然后執行SQL語句獲取數據,并通過循環將數據存儲在一個數組中。最后調用ConvertToJson函數將數組轉換為JSON格式,并將JSON數據響應給客戶端。