日期:2014-05-16  浏览次数:20525 次

ASP如何讲查询结果打印成可转换成JSON的字符串
ASP如何讲查询结果打印成可转换成JSON的字符串
sql="select id,code,date from table where del=0 "
rs.Open (sql,conn,adOpenForwardOnly,adLockReadOnly,adCmdText);
if(!rs.eof){
//response.write("{ \"name\": \"Violet\", \"occupation\": \"character\" }")
  //上面这句我已经能单独alert Violet,也就是说我的各方面配置都是可以了的。
response.write("{ \"fid\": '"+rs("fid")+"', \"code\": '"+rs("code")+"', \"sdate\": '"+rs("sdate")+"' }")
  大家帮我看下这句语法是不是对的?
大家有没有ASP能把RS结果集转变成可以转变成JSON的字符串,用JSCRIPT语法,不用VBSCRIPT,
然后我用AJAX打到前台,JS处理,感觉这样应该会比较有实用价值的啊。
现在我都是只能传1个值过来,很惨的,



------解决方案--------------------
属性也用"包,然后处理一下数据库里读出来内容的引号就行,你这个应该没什么别的问题
------解决方案--------------------
网上去下个 json 类来

直接可以转换成 数据转换成 json格式
------解决方案--------------------
Const JSON_OBJECT = 0
Const JSON_ARRAY = 1

Class jsCore
Public Collection
Public Count
Public QuotedVars
Public Kind ' 0 = object, 1 = array

Private Sub Class_Initialize
Set Collection = CreateObject("Scripting.Dictionary")
QuotedVars = True
Count = 0
End Sub

Private Sub Class_Terminate
Set Collection = Nothing
End Sub

' counter
Private Property Get Counter 
Counter = Count
Count = Count + 1
End Property

' - data maluplation
' -- pair
Public Property Let Pair(p, v)
If IsNull(p) Then p = Counter
Collection(p) = v
End Property

Public Property Set Pair(p, v)
If IsNull(p) Then p = Counter
If TypeName(v) <> "jsCore" Then
Err.Raise &hD, "class: class", "Incompatible types: '" & TypeName(v) & "'"
End If
Set Collection(p) = v
End Property

Public Default Property Get Pair(p)
If IsNull(p) Then p = Count - 1
If IsObject(Collection(p)) Then
Set Pair = Collection(p)
Else
Pair = Collection(p)
End If
End Property
' -- pair
Public Sub Clean
Collection.RemoveAll
End Sub

Public Sub Remove(vProp)
Collection.Remove vProp
End Sub
' data maluplation

' encoding
Function jsEncode(str)
Dim charmap(127), haystack()
charmap(8) = "\b"
charmap(9) = "\t"
charmap(10) = "\n"
charmap(12) = "\f"
charmap(13) = "\r"
charmap(34) = "\"""
charmap(47) = "\/"
charmap(92) = "\\"

Dim strlen : strlen = Len(str) - 1
ReDim haystack(strlen)

Dim i, charcode
For i = 0 To strlen
haystack(i) = Mid(str, i + 1, 1)

charcode = AscW(haystack(i)) And 65535
If charcode < 127 Then
If Not IsEmpty(charmap(charcode)) Then
haystack(i) = charmap(charcode)
ElseIf charcode < 32 Then
haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
End If
Else
haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
End If
Next

jsEncode = Join(haystack, "")
End Function

' converting
Public Function toJSON(vPair)
Select Case VarType(vPair)
Case 0 ' Empty
toJSON = "null"
Case 1 ' Null
toJSON = "null"
Case 7 ' Date
' toJSON = "new Date(" & (vPair - CDate(25569)) * 86400000 & ")" ' let in