Json for asp

 

  1'
  2'    VBS JSON 2.0.2
  3'    Copyright (c) 2008 Turul Topuz
  4'    Under the MIT (MIT-LICENSE.txt) license.
  5'
  6
  7Const JSON_OBJECT    = 0
  8Const JSON_ARRAY    = 1
  9
 10Class jsCore
 11    Public Collection
 12    Public Count
 13    Public QuotedVars
 14    Public Kind ' 0 = object, 1 = array
 15
 16    Private Sub Class_Initialize
 17        Set Collection = CreateObject("Scripting.Dictionary")
 18        QuotedVars = True
 19        Count = 0
 20    End Sub
 21
 22    Private Sub Class_Terminate
 23        Set Collection = Nothing
 24    End Sub
 25
 26    ' counter
 27    Private Property Get Counter 
 28        Counter = Count
 29        Count = Count + 1
 30    End Property
 31
 32    ' - data maluplation
 33    ' -- pair
 34    Public Property Let Pair(p, v)
 35        If IsNull(p) Then p = Counter
 36        Collection(p) = v
 37    End Property
 38
 39    Public Property Set Pair(p, v)
 40        If IsNull(p) Then p = Counter
 41        If TypeName(v) <> "jsCore" Then
 42            Err.Raise &hD, "class: class""Tr uyumsuz: '" & TypeName(v) & "'"
 43        End If
 44        Set Collection(p) = v
 45    End Property
 46
 47    Public Default Property Get Pair(p)
 48        If IsNull(p) Then p = Count - 1
 49        If IsObject(Collection(p)) Then
 50            Set Pair = Collection(p)
 51        Else
 52            Pair = Collection(p)
 53        End If
 54    End Property
 55    ' -- pair
 56    Public Sub Clean
 57        Collection.RemoveAll
 58    End Sub
 59
 60    Public Sub Remove(vProp)
 61        Collection.Remove vProp
 62    End Sub
 63    ' data maluplation
 64
 65    ' encoding
 66    Function jsEncode(str)
 67        Dim i, j, aL1, aL2, c, p
 68
 69        aL1 = Array(&h22, &h5C, &h2F, &h08, &h0C, &h0A, &h0D, &h09)
 70        aL2 = Array(&h22, &h5C, &h2F, &h62, &h66, &h6E, &h72, &h74)
 71        For i = 1 To Len(str)
 72            p = True
 73            c = Mid(str, i, 1)
 74            For j = 0 To 7
 75                If c = Chr(aL1(j)) Then
 76                    jsEncode = jsEncode & "\" & Chr(aL2(j))
 77                    p = False
 78                    Exit For
 79                End If
 80            Next
 81
 82            If p Then 
 83                Dim a
 84                a = AscW(c)
 85                If a > 31 And a < 127 Then
 86                    jsEncode = jsEncode & c
 87                ElseIf a > -1 Or a < 65535 Then
 88                    jsEncode = jsEncode & "\u" & String(4 - Len(Hex(a)), "0"& Hex(a)
 89                End If 
 90            End If
 91        Next
 92    End Function
 93
 94    ' converting
 95    Public Function toJSON(vPair)
 96        Select Case VarType(vPair)
 97            Case 1    ' Null
 98                toJSON = "null"
 99            Case 7    ' Date
100                ' yaz saati problemi var
101                ' jsValue = "new Date(" & Round((vVal - #01/01/1970 02:00#) * 86400000) & ")"
102                toJSON = """" & CStr(vPair) & """"
103            Case 8    ' String
104                toJSON = """" & jsEncode(vPair) & """"
105            Case 9    ' Object
106                Dim bFI,i 
107                bFI = True
108                If vPair.Kind Then toJSON = toJSON & "[" Else toJSON = toJSON & "{"
109                For Each i In vPair.Collection
110                    If bFI Then bFI = False Else toJSON = toJSON & ","
111
112                    If vPair.Kind Then 
113                        toJSON = toJSON & toJSON(vPair(i))
114                    Else
115                        If QuotedVars Then
116                            toJSON = toJSON & """" & i & """:" & toJSON(vPair(i))
117                        Else
118                            toJSON = toJSON & i & ":" & toJSON(vPair(i))
119                        End If
120                    End If
121                Next
122                If vPair.Kind Then toJSON = toJSON & "]" Else toJSON = toJSON & "}"
123            Case 11
124                If vPair Then toJSON = "true" Else toJSON = "false"
125            Case 1281928204
126                Dim sEB
127                toJSON = MultiArray(vPair, 1"", sEB)
128            Case Else
129                toJSON = Replace(vPair, ","".")
130        End select
131    End Function
132
133    Function MultiArray(aBD, iBC, sPS, ByRef sPT)    ' Array BoDy, Integer BaseCount, String PoSition
134        Dim iDU, iDL, i    ' Integer DimensionUBound, Integer DimensionLBound
135        On Error Resume Next
136        iDL = LBound(aBD, iBC)
137        iDU = UBound(aBD, iBC)
138        
139        Dim sPB1, sPB2    ' String PointBuffer1, String PointBuffer2
140        If Err = 9 Then
141            sPB1 = sPT & sPS
142            For i = 1 To Len(sPB1)
143                If i <> 1 Then sPB2 = sPB2 & ","
144                sPB2 = sPB2 & Mid(sPB1, i, 1)
145            Next
146            MultiArray = MultiArray & toJSON(Eval("aBD(" & sPB2 & ")"))
147        Else
148            sPT = sPT & sPS
149            MultiArray = MultiArray & "["
150            For i = iDL To iDU
151                MultiArray = MultiArray & MultiArray(aBD, iBC + 1, i, sPT)
152                If i < iDU Then MultiArray = MultiArray & ","
153            Next
154            MultiArray = MultiArray & "]"
155            sPT = Left(sPT, iBC - 2)
156        End If
157    End Function
158
159    Public Property Get jsString
160        jsString = toJSON(Me)
161    End Property
162
163    Sub Flush
164        If TypeName(Response) <> "Empty" Then 
165            Response.Write(jsString)
166        ElseIf WScript <> Empty Then 
167            WScript.Echo(jsString)
168        End If
169    End Sub
170
171    Public Function Clone
172        Set Clone = ColClone(Me)
173    End Function
174
175    Private Function ColClone(core)
176        Dim jsc, i
177        Set jsc = new jsCore
178        jsc.Kind = core.Kind
179        For Each i In core.Collection
180            If IsObject(core(i)) Then
181                Set jsc(i) = ColClone(core(i))
182            Else
183                jsc(i) = core(i)
184            End If
185        Next
186        Set ColClone = jsc
187    End Function
188
189End Class
190
191Function jsObject
192    Set jsObject = new jsCore
193    jsObject.Kind = JSON_OBJECT
194End Function
195
196Function jsArray
197    Set jsArray = new jsCore
198    jsArray.Kind = JSON_ARRAY
199End Function
200
201Function toJSON(val)
202    toJSON = (new jsCore).toJSON(val)
203End Function

 

 

补充:

Function QueryToJSON(dbc, sql)
        Dim rs, jsa
        Set rs = dbc.Execute(sql)
        Set jsa = jsArray()
        While Not (rs.EOF Or rs.BOF)
                Set jsa(Null) = jsObject()
                For Each col In rs.Fields
                        jsa(Null)(col.Name) = col.Value
                Next
        rs.MoveNext
        Wend
        Set QueryToJSON = jsa
End Function

 

posted on 2008-11-01 16:11  胖兔子  阅读(1787)  评论(0)    收藏  举报

导航