用asp怎么将查询结果转化为json
2个回答
展开全部
你说的是ASP VBS吗?
我这里有一个json编码函数
'Json编码 by Shirne
'支持数组,Dictionary,Recordset等对象直接编码,支持数组中 key&":"&value格式
Function JsonEncode(json)
Dim i, l, key, str
str = ""
If IsArray(json) Then
l = UBound(json)
If TypeName(json(0))="String" Then
If InStr(json(0),":")>0 Then
str = str & "{"
For i=0 To l
str = str & """" & Left(json(i),InStr(json(i),":")-1) &""":"
str = str & """" & EncodeJSON(Mid(json(i),InStr(json(i),":")+1)) &""""
If i<l Then str = str & ","
Next
str = str & "}"
JsonEncode = str
Exit Function
End If
End If
str = str & "["
For i=0 To l
str = str & JsonEncode(json(i))
If i<l Then str = str & ","
Next
str = str & "]"
ElseIf TypeName(json)="Dictionary" Then
l = json.Count
i = 0
str = str & "{"
For Each key In json
i = i + 1
str = str & """" & key &""":"
str = str & JsonEncode(json(key))
If i<l Then str = str & ","
Next
str = str & "}"
ElseIf TypeName(json)="Recordset" Then
Dim Field
l = json.Fields.Count
str = str & "["
Do Until json.EOF
i = 0
str = str & "{"
For Each Field In json.Fields
i = i + 1
str = str & """" & LCase(Field.Name) &""":"
'If UCase(Field.Name)="IP" Then
' str = str & JsonEncode(IPMask(Field.Value))
'Else
str = str & JsonEncode(Field.Value)
'End If
If i<l Then str = str & ","
Next
str = str & "}"
json.MoveNext
If Not json.EOF Then str = str & ","
Loop
str = str & "]"
Else
Select Case VarType(json)
Case 2,3,4,5,6
str = str & json
Case Else
str = str & """" & EncodeJSON(json&"") &""""
End Select
End If
JsonEncode = str
End Function
用法:
Response.Write JsonEncode(Array("state:"&stat, "message:"&msg, "url:"¶m))
//或
Response.Write JsonEncode(dictionaryObject)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询