zoukankan      html  css  js  c++  java
  • Asp中JSON的使用

    测试用例中使用到了一个lib文件:

    Json_2.0.3.asp


    <%
    '
    ' VBS JSON 2.0.3
    ' Copyright (c) 2009 Tu餽ul Topuz
    ' Under the MIT (MIT-LICENSE.txt) license.
    '

    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) = ""
    charmap(9) = " "
    charmap(10) = " "
    charmap(12) = "f"
    charmap(13) = " "
    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 only utc time
    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, 8192, 8204
    toJSON = RenderArray(vPair, 1, "")
    Case Else
    toJSON = Replace(vPair, ",", ".")
    End select
    End Function

    Function RenderArray(arr, depth, parent)
    Dim first : first = LBound(arr, depth)
    Dim last : last = UBound(arr, depth)

    Dim index, rendered
    Dim limiter : limiter = ","

    RenderArray = "["
    For index = first To last
    If index = last Then
    limiter = ""
    End If

    On Error Resume Next
    rendered = RenderArray(arr, depth + 1, parent & index & "," )

    If Err = 9 Then
    On Error GoTo 0
    RenderArray = RenderArray & toJSON(Eval("arr(" & parent & index & ")")) & limiter
    Else
    RenderArray = RenderArray & rendered & "" & limiter
    End If
    Next
    RenderArray = RenderArray & "]"
    End Function

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

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

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

    Private Function ColClone(core)
    Dim jsc, i
    Set jsc = new jsCore
    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

    End Class

    Function jsObject
    Set jsObject = new jsCore
    jsObject.Kind = JSON_OBJECT
    End Function

    Function jsArray
    Set jsArray = new jsCore
    jsArray.Kind = JSON_ARRAY
    End Function

    Function toJSON(val)
    toJSON = (new jsCore).toJSON(val)
    End Function
    %>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    测试用例:


    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>

    <%

    Response.ContentType = "text/JSON"
    %>
    <!--#include file="Json_2.0.3.asp"-->
    <%
    Dim ar
    Set ar = jsArray()
    Dim Jsons
    Set Jsons = jsObject()
    Jsons("Name") = "1"
    Jsons("Age") = 10


    ar(0)=Jsons.jsString
    Jsons("Name") = "2"
    Jsons("Age") = 20
    ar(1)=Jsons.jsString
    Jsons("Name") = "3"
    Jsons("Age") = 30
    ar(2)=Jsons.jsString
    Jsons("Name") = "4"
    Jsons("Age") = 40
    ar(3)=Jsons.jsString
    Jsons("Name") = "5"
    Jsons("Age") = 50
    ar(4)=Jsons.jsString
    Jsons("Name") = "6"
    Jsons("Age") = 60
    ar(5)=Jsons.jsString
    Jsons("Name") = "7"
    Jsons("Age") = 70
    ar(6)=Jsons.jsString
    Jsons("Name") = "8"
    Jsons("Age") = 80
    ar(7)=Jsons.jsString
    Jsons("Name") = "9"
    Jsons("Age") = 90
    ar(8)=Jsons.jsString
    Jsons("Name") = "10"
    Jsons("Age") = 100
    ar(9)=Jsons.jsString
    'Response.write ar(0)
    'Response.write ar(1)
    'Response.write ar(2)
    Response.write ar.jsString

    Response.Write "<BR>"


    DIM myArray
    myArray = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct", "Nov","Dec")
    'Response.write myArray(0)

    Dim sc
    Set sc = CreateObject("MSScriptControl.ScriptControl")
    Dim str
    'str = "{'uid':'1','username':'abc','email':'123@163.com'}"
    str="['aaa','bbb','ccc']"
    sc.Language = "JScript"
    sc.AddCode "var o = " & str & ";"
    Response.Write sc.Eval("o[1]")


    Set Jsons = Nothing

    %>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    由于对asp了解太少,文章写的不够全面,不过这已经可以满足很多json的结构了。
    ---------------------
    作者:Cool-span
    来源:CSDN
    原文:https://blog.csdn.net/qxs965266509/article/details/46873175
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    网址收藏
    Linux创建swap文件
    vim命令大全
    char * 和字符数组
    JSR 203终于要出来啦
    对象关系技术的探讨
    最近编码更流畅了
    孤独终止的地方,就是广场开始的地方......
    不要奢望.NET能够跨平台
    实现了HTTP多线程下载
  • 原文地址:https://www.cnblogs.com/apolloren/p/10424998.html
Copyright © 2011-2022 走看看