Asp json source code sharing

Source: Internet
Author: User

Copy codeThe Code is as follows: <%
'================================================ ==================================
'File name:/Cls_Json.asp
'File function: System JSON files
'File Version: vbs json (JavaScript Object Notation) Version 2.0.2
'Program modification: Cloud. L
'Last updated:
'================================================ ==================================
'Program core: Official JSON http://www.json.org/
'Author's blog: Http: // www.cnode.cn
'================================================ ==================================
Class Json_Cls

Public Collection
Public Count
Public QuotedVars 'whether to add quotation marks for variables
Public Kind '0 = object, 1 = array

Private Sub Class_Initialize
Set Collection = Server. CreateObject (GP_ScriptingDictionary)
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

'Set the object type
Public Property Let SetKind (ByVal fpKind)
Select Case LCase (fpKind)
Case "object": Kind = 0
Case "array": Kind = 1
End Select
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) <> "Json_Cls" Then
Err. Raise & hD, "class: class", "class object: '" & 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'
Public Function jsEncode (str)
Dim I, j, aL1, Al, c, p

AL1 = Array (& h08, & h5C, & h2F, & h08, & h0C, & h0A, & h0D, & h09)
Al 2 = Array (& h22, & h5C, & h2F, & h62, & h66, & h6E, & h72, & h74)
For I = 1 To Len (str)
P = True
C = Mid (str, I, 1)
For j = 0 To 7
If c = Chr (aL1 (j) Then
JsEncode = jsEncode & "\" & Chr (Al (j ))
P = False
Exit
End If
Next

If p Then
Dim
A = AscW (c)
If a> 31 And a <127 Then
JsEncode = jsEncode & c
ElseIf a>-1 Or a <65535 Then
JsEncode = jsEncode & "\ u" & String (4-Len (Hex (a), "0") & Hex ()
End If
End If
Next
End Function

'Converting
Public Function toJSON (vPair)
Select Case VarType (vPair)
Case 1 'null'
ToJSON = "null"
Case 7 'date'
'Yaz saati problemi var
'Jsvalue = "new Date (" & Round (vVal-#01/01/1970 #) * 86400000 )&")"
ToJSON = "& CStr (vPair )&""""
Case 8 'string'
ToJSON = "& jsEncode (vPair )&""""
Case 9 'object
Dim bFI, I
BFI = True
If vPair. Kind Then toJSON = toJSON & "[" Else toJSON = toJSON &"{"
For Each I In vPair. Collection
If bFI Then bFI = False Else toJSON = toJSON &","

If vPair. Kind Then
ToJSON = toJSON & toJSON (vPair (I ))
Else
If QuotedVars Then
ToJSON = toJSON & "& I &" ":" & toJSON (vPair (I ))
Else
ToJSON = toJSON & I & ":" & toJSON (vPair (I ))
End If
End If
Next
If vPair. Kind Then toJSON = toJSON & "]" Else toJSON = toJSON &"}"
Case 11
If vPair Then toJSON = "true" Else toJSON = "false"
Case 12,819 2, 8204
Dim sEB
ToJSON = MultiArray (vPair, 1, "", sEB)
Case Else
ToJSON = Replace (vPair ,",",".")
End select
End Function

Public Function MultiArray (aBD, iBC, sPS, ByRef sPT) 'array BoDy, Integer BaseCount, String PoSition
Dim iDU, iDL, I 'integer DimensionUBound, Integer DimensionLBound
On Error Resume Next
IDL = LBound (aBD, iBC)
IDU = UBound (aBD, iBC)

Dim sPB1, sPB2 'string PointBuffer1, String PointBuffer2
If Err = 9 Then
SPB1 = sPT & sPS
For I = 1 To Len (sPB1)
If I <> 1 Then sPB2 = sPB2 &","
SPB2 = sPB2 & Mid (sPB1, I, 1)
Next
MultiArray = MultiArray & toJSON (Eval ("aBD (" & sPB2 &")"))
Else
SPT = sPT & sPS
MultiArray = MultiArray &"["
For I = iDL To iDU
MultiArray = MultiArray & MultiArray (aBD, iBC + 1, I, sPT)
If I <iDU Then MultiArray = MultiArray &","
Next
MultiArray = MultiArray & "]"
SPT = Left (sPT, iBC-2)
End If
End Function

Public Property Get ToString
ToString = toJSON (Me)
End Property

Public Sub Flush
If TypeName (Response) <> "Empty" Then
Response. Write (ToString)
ElseIf WScript <> Empty Then
WScript. Echo (ToString)
End If
End Sub

Public Function Clone
Set Clone = ColClone (Me)
End Function

Private Function ColClone (core)
Dim jsc, I
Set jsc = New Json_Cls
Jsc. Kind = core. Kind
For Each I In core. Collection
If IsObject (core (I) Then
Set jsc (I) = ColClone (core (I ))
Else
Jsc (I) = core (I)
End If
Next
Set ColClone = jsc
End Function

Public Function QueryToJSON (dbc, SQL)
Dim rs, jsa, col
Set rs = dbc. Execute (SQL)
Set jsa = New Json_Cls
Jsa. SetKind = "array"
While Not (rs. EOF Or rs. BOF)
Set jsa (Null) = New Json_Cls
Jsa (Null). SetKind = "object"
For Each col In rs. Fields
Jsa (Null) (col. Name) = col. Value
Next
Rs. MoveNext
Wend
Set QueryToJSON = jsa
End Function

End Class
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.