asp使用AspJson转换json数据

需要使用json数据的场景很多,那么asp中如何操作json数据呢?asp与json格式又如何交互呢?本文介绍使用AspJson转换为json格式数据输出,以及解析json数据。

aspJSON1.17.asp文件

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
215
0 Then
                aj_in_string = False
                aj_in_escape = False
                aj_colonfound = False
                For aj_i_tmp = 1 To Len(aj_line)
                    If aj_in_escape Then
                        aj_in_escape = False
                    Else
                        Select Case Mid(aj_line, aj_i_tmp, 1)
                            Case """"
                                aj_in_string = Not aj_in_string
                            Case ":"
                                If Not aj_in_escape And Not aj_in_string Then
                                    aj_currentkey = Left(aj_line, aj_i_tmp - 1)
                                    aj_currentvalue = Mid(aj_line, aj_i_tmp + 1)
                                    aj_colonfound = True
                                    Exit For
                                End If
                            Case ""
                                aj_in_escape = True
                        End Select
                    End If
                Next
                if aj_colonfound then
                    aj_currentkey = aj_Strip(aj_JSONDecode(aj_currentkey), """")
                    If Not level(aj_currentlevel).exists(aj_currentkey) Then level(aj_currentlevel).Add aj_currentkey, ""
                end if
            End If
            If right(aj_line,1) = "{" Or right(aj_line,1) = "[" Then
                If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
                Set level(aj_currentlevel).Item(aj_currentkey) = Collection()
                Set level(aj_currentlevel + 1) = level(aj_currentlevel).Item(aj_currentkey)
                aj_currentlevel = aj_currentlevel + 1
                aj_currentkey = ""
            ElseIf right(aj_line,1) = "}" Or right(aj_line,1) = "]" or right(aj_line,2) = "}," Or right(aj_line,2) = "]," Then
                aj_currentlevel = aj_currentlevel - 1
            ElseIf Len(Trim(aj_line)) > 0 Then
                if Len(aj_currentvalue) = 0 Then aj_currentvalue = aj_line
                aj_currentvalue = getJSONValue(aj_currentvalue)
  
                If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
                level(aj_currentlevel).Item(aj_currentkey) = aj_currentvalue
            End If
        Next
    End Sub
  
    Public Function Collection()
        set Collection = Server.CreateObject("Scripting.Dictionary")
    End Function
  
    Public Function AddToCollection(dictobj)
        if TypeName(dictobj)  "Dictionary" then Err.Raise 1, "AddToCollection Error", "Not a collection."
        aj_newlabel = dictobj.Count
        dictobj.Add aj_newlabel, Collection()
        set AddToCollection = dictobj.item(aj_newlabel)
    end function
  
    Private Function CleanUpJSONstring(aj_originalstring)
        aj_originalstring = Replace(aj_originalstring, Chr(13) & Chr(10), "")
        aj_originalstring = Mid(aj_originalstring, 2, Len(aj_originalstring) - 2)
        aj_in_string = False : aj_in_escape = False : aj_s_tmp = ""
        For aj_i_tmp = 1 To Len(aj_originalstring)
            aj_char_tmp = Mid(aj_originalstring, aj_i_tmp, 1)
            If aj_in_escape Then
                aj_in_escape = False
                aj_s_tmp = aj_s_tmp & aj_char_tmp
            Else
                Select Case aj_char_tmp
                    Case "" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_escape = True
                    Case """" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_string = Not aj_in_string
                    Case "{", "["
                        aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))
                    Case "}", "]"
                        aj_s_tmp = aj_s_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) & aj_char_tmp
                    Case "," : aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))
                    Case Else : aj_s_tmp = aj_s_tmp & aj_char_tmp
                End Select
            End If
        Next
  
        CleanUpJSONstring = ""
        aj_s_tmp = split(aj_s_tmp, Chr(13) & Chr(10))
        For Each aj_line_tmp In aj_s_tmp
            aj_line_tmp = replace(replace(aj_line_tmp, chr(10), ""), chr(13), "")
            CleanUpJSONstring = CleanUpJSONstring & aj_Trim(aj_line_tmp) & Chr(13) & Chr(10)
        Next
    End Function
  
    Private Function getJSONValue(ByVal val)
        val = Trim(val)
        If Left(val,1) = ":"  Then val = Mid(val, 2)
        If Right(val,1) = "," Then val = Left(val, Len(val) - 1)
        val = Trim(val)
  
        Select Case val
            Case "true"  : getJSONValue = True
            Case "false" : getJSONValue = False
            Case "null" : getJSONValue = Null
            Case Else
                If (Instr(val, """") = 0) Then
                    If IsNumeric(val) Then
                        getJSONValue = CDbl(val)
                    Else
                        getJSONValue = val
                    End If
                Else
                    If Left(val,1) = """" Then val = Mid(val, 2)
                    If Right(val,1) = """" Then val = Left(val, Len(val) - 1)
                    getJSONValue = aj_JSONDecode(Trim(val))
                End If
        End Select
    End Function
  
    Private JSONoutput_level
    Public Function JSONoutput()
        dim wrap_dicttype, aj_label
        JSONoutput_level = 1
        wrap_dicttype = "[]"
        For Each aj_label In data
             If Not aj_IsInt(aj_label) Then wrap_dicttype = "{}"
        Next
        JSONoutput = Left(wrap_dicttype, 1) & Chr(13) & Chr(10) & GetDict(data) & Right(wrap_dicttype, 1)
    End Function
  
    Private Function GetDict(objDict)
        dim aj_item, aj_keyvals, aj_label, aj_dicttype
        For Each aj_item In objDict
            Select Case TypeName(objDict.Item(aj_item))
                Case "Dictionary"
                    GetDict = GetDict & Space(JSONoutput_level * 4)
                     
                    aj_dicttype = "[]"
                    For Each aj_label In objDict.Item(aj_item).Keys
                         If Not aj_IsInt(aj_label) Then aj_dicttype = "{}"
                    Next
                    If aj_IsInt(aj_item) Then
                        GetDict = GetDict & (Left(aj_dicttype,1) & Chr(13) & Chr(10))
                    Else
                        GetDict = GetDict & ("""" & aj_JSONEncode(aj_item) & """" & ": " & Left(aj_dicttype,1) & Chr(13) & Chr(10))
                    End If
                    JSONoutput_level = JSONoutput_level + 1
                     
                    aj_keyvals = objDict.Keys
                    GetDict = GetDict & (GetSubDict(objDict.Item(aj_item)) & Space(JSONoutput_level * 4) & Right(aj_dicttype,1) & aj_InlineIf(aj_item = aj_keyvals(objDict.Count - 1),"" , ",") & Chr(13) & Chr(10))
                Case Else
                    aj_keyvals =  objDict.Keys
                    GetDict = GetDict & (Space(JSONoutput_level * 4) & aj_InlineIf(aj_IsInt(aj_item), "", """" & aj_JSONEncode(aj_item) & """: ") & WriteValue(objDict.Item(aj_item)) & aj_InlineIf(aj_item = aj_keyvals(objDict.Count - 1),"" , ",") & Chr(13) & Chr(10))
            End Select
        Next
    End Function
  
    Private Function aj_IsInt(val)
        aj_IsInt = (TypeName(val) = "Integer" Or TypeName(val) = "Long")
    End Function
  
    Private Function GetSubDict(objSubDict)
        GetSubDict = GetDict(objSubDict)
        JSONoutput_level= JSONoutput_level -1
    End Function
  
    Private Function WriteValue(ByVal val)
        Select Case TypeName(val)
            Case "Double", "Integer", "Long": WriteValue = val
            Case "Null"                     : WriteValue = "null"
            Case "Boolean"                  : WriteValue = aj_InlineIf(val, "true", "false")
            Case Else                       : WriteValue = """" & aj_JSONEncode(val) & """"
        End Select
    End Function
  
    Private Function aj_JSONEncode(ByVal val)
        val = Replace(val, "", "\")
        val = Replace(val, """", """")
        'val = Replace(val, "/", "/")
        val = Replace(val, Chr(8), "b")
        val = Replace(val, Chr(12), "f")
        val = Replace(val, Chr(10), "n")
        val = Replace(val, Chr(13), "r")
        val = Replace(val, Chr(9), "t")
        aj_JSONEncode = Trim(val)
    End Function
  
    Private Function aj_JSONDecode(ByVal val)
        val = Replace(val, """", """")
        val = Replace(val, "\", "")
        val = Replace(val, "/", "/")
        val = Replace(val, "b", Chr(8))
        val = Replace(val, "f", Chr(12))
        val = Replace(val, "n", Chr(10))
        val = Replace(val, "r", Chr(13))
        val = Replace(val, "t", Chr(9))
        aj_JSONDecode = Trim(val)
    End Function
  
    Private Function aj_InlineIf(condition, returntrue, returnfalse)
        If condition Then aj_InlineIf = returntrue Else aj_InlineIf = returnfalse
    End Function
  
    Private Function aj_Strip(ByVal val, stripper)
        If Left(val, 1) = stripper Then val = Mid(val, 2)
        If Right(val, 1) = stripper Then val = Left(val, Len(val) - 1)
        aj_Strip = val
    End Function
  
    Private Function aj_MultilineTrim(TextData)
        aj_MultilineTrim = aj_RegExp.Replace(TextData, "$1")
    End Function
  
    private function aj_Trim(val)
        aj_Trim = Trim(val)
        Do While Left(aj_Trim, 1) = Chr(9) : aj_Trim = Mid(aj_Trim, 2) : Loop
        Do While Right(aj_Trim, 1) = Chr(9) : aj_Trim = Left(aj_Trim, Len(aj_Trim) - 1) : Loop
        aj_Trim = Trim(aj_Trim)
    end function
End Class
%>

AspJson官方使用方法参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"
  
'Loop through collection
For Each phonenr In oJSON.data("phoneNumber")
    Set this = oJSON.data("phoneNumber").item(phonenr)
    Response.Write _
    this.item("type") & ": " & _
    this.item("number") & "<br>"
Next
  
'Update/Add value
oJSON.data("firstName") = "James"
  
'Return json string
Response.Write oJSON.JSONoutput()
%>

示例一—输出json

1
 

示例二—转换为json格式输出

读取数据库中article表数据,转换为json格式数据输出

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
dim oJSON
    Set oJSON = New aspJSON
    sql="Select * From article where IsSelected=1 and getdate() between isnull(showBeginTime,'1900-1-1 00:00:00') and isnull(showEndTime,getdate()) and (','+UserTypeNo+',' like '%,"&OAUserTypeNo&",%' or UserTypeNo='')"
    'response.Write(sql)
    set rs=server.CreateObject("adodb.recordset")
    OpenConn : rs.open sql,Conn,1,3
    dim n
    n=0
     
    With oJSON.data
    do while not rs.eof
     
         
        .Add n, oJSON.Collection()
        With .item(n)
        .Add "ID", ""&rs("ID")&""
        .Add "Title", ""&rs("Title")&""
        .Add "IsActivityTime", ""&rs("IsActivityTime")&""
        .Add "activityBeginTime", ""&rs("activityBeginTime")&""
        .Add "activityEndTime", ""&rs("activityEndTime")&""                      'Create value
        End With
        n=n+1
    rs.movenext
    loop
    End With   
    Response.Write oJSON.JSONoutput()                   'Return json string

示例三—JS读取Json

前端js使用ajax调用后端接口,获取到上面的json数据,解析输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var postData="Action=Announce"
    //alert(postData)
        digtalMessage="";
        digtalMessageAll="";
  
    $.ajax({
       type: "GET",
       url: "/OKOA/CN/AjaxInfo.asp",
       data: postData,
       success: function(msg){
        //alert( "Data Saved: " + msg );
         
        var obj = jQuery.parseJSON(msg);
        $.each(obj,function(idx,item){    
           //alert(item.activityBeginTime)
            digtalMessage=DigitalTime1(item.Title,item.Title,item.activityBeginTime,item.activityEndTime);
             
            digtalMessageAll=digtalMessageAll+" <a class=""Default"" href="%5C%22#%5C%22">"+digtalMessage+"</a>;"
        })
        document.getElementById("LiveClock1").innerHTML=digtalMessageAll;
       }
    });

总结

到此这篇关于asp使用AspJson转换json数据的文章就介绍到这了,更多相关asp中的AspJson内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/asp-net/9377.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部