├── README.md ├── aspJSON1.18.asp └── aspJSON1.19.asp /README.md: -------------------------------------------------------------------------------- 1 | # aspjson 2 | 3 | Classic ASP JSON Class Reading/Writing 4 | 5 | [ASPJSON](https://www.aspjson.com) is a free to use project for generating and reading JSON data into a classic ASP object. 6 | The class can be used for reading a string of JSON data as well as writing JSON output from an AJAX file. 7 | Below are 2 simple examples of both. 8 | 9 | ## Read Json Example: 10 | 11 | ``` 12 | { 13 | "firstName": "John", 14 | "lastName" : "Smith", 15 | "age" : 25, 16 | "address" : 17 | { 18 | "streetAddress": "21 2nd Street", 19 | "city" : "New York", 20 | "state" : "NY", 21 | "postalCode" : "10021" 22 | }, 23 | "phoneNumbers": 24 | [ 25 | { 26 | "type" : "home", 27 | "number": "212 555-1234" 28 | }, 29 | { 30 | "type" : "fax", 31 | "number": "646 555-4567" 32 | } 33 | ] 34 | } 35 | ``` 36 | 37 | ``` 38 | <% 39 | Set oJSON = New aspJSON 40 | 41 | 'Load JSON string 42 | oJSON.loadJSON(jsonstring) 43 | 44 | 'Get single value 45 | Response.Write oJSON.data("firstName") & "
" 46 | Response.Write oJSON.data("address").item("streetAddress") & "
" 47 | 48 | 'Loop through collection 49 | For Each phonenr In oJSON.data("phoneNumbers") 50 | Set this = oJSON.data("phoneNumbers").item(phonenr) 51 | Response.Write _ 52 | this.item("type") & ": " & _ 53 | this.item("number") & "
" 54 | Next 55 | 56 | 'Update/Add value 57 | oJSON.data("firstName") = "James" 58 | 59 | 'Return json string 60 | Response.Write oJSON.JSONoutput() 61 | %> 62 | ``` 63 | 64 | 65 | ## Write Json Example: 66 | 67 | ``` 68 | { 69 | "familyName": "Smith", 70 | "familyMembers": 71 | [ 72 | { 73 | "firstName": "John", 74 | "age": 41, 75 | "job": 76 | { 77 | "function": "Webdeveloper", 78 | "salary": 70000 79 | } 80 | }, 81 | { 82 | "firstName": "Suzan", 83 | "age": 38, 84 | "interests": 85 | [ 86 | "Reading", 87 | "Tennis", 88 | "Painting" 89 | ] 90 | }, 91 | { 92 | "firstName": "John Jr.", 93 | "age": 2.5 94 | } 95 | ] 96 | } 97 | ``` 98 | 99 | ``` 100 | <% 101 | Set oJSON = New aspJSON 102 | 103 | With oJSON.data 104 | 105 | .Add "familyName", "Smith" 'Create value 106 | .Add "familyMembers", oJSON.Collection() 107 | 108 | With oJSON.data("familyMembers") 109 | 110 | .Add 0, oJSON.Collection() 'Create unnamed object 111 | With .item(0) 112 | .Add "firstName", "John" 113 | .Add "age", 41 114 | 115 | .Add "job", oJSON.Collection() 'Create named object 116 | With .item("job") 117 | .Add "function", "Webdeveloper" 118 | .Add "salary", 70000 119 | End With 120 | End With 121 | 122 | 123 | .Add 1, oJSON.Collection() 124 | With .item(1) 125 | .Add "firstName", "Suzan" 126 | .Add "age", 38 127 | .Add "interests", oJSON.Collection() 'Create array 128 | With .item("interests") 129 | .Add 0, "Reading" 130 | .Add 1, "Tennis" 131 | .Add 2, "Painting" 132 | End With 133 | End With 134 | 135 | .Add 2, oJSON.Collection() 136 | With .item(2) 137 | .Add "firstName", "John Jr." 138 | .Add "age", 2.5 139 | End With 140 | 141 | End With 142 | 143 | End With 144 | 145 | Response.Write oJSON.JSONoutput() 'Return json string 146 | ``` 147 | 148 | 149 | ## Changelog 150 | 151 | ### Version 1.19 January 2021 152 | * Fix for locale decimal format settings when parsing json string 153 | 154 | ### Version 1.18 August 2020 155 | * Decimal output comma/dot fix 156 | 157 | ### Version 1.17 Februari 2014 158 | * Efficiency improvement large data 159 | 160 | ### Version 1.15 Februari 2014 161 | * Trailing tabs fixed 162 | 163 | ### Version 1.14 Februari 2014 164 | * Colon value within string bug fixed 165 | 166 | ### Version 1.13 December 2013 167 | * Encoded data fix 168 | * Now possible to load directly from a URL. For example: oJSON.loadJSON("http://www.aspjson.com/jsonstream.asp") 169 | 170 | ### Version 1.12 June 2013 171 | * vbCrLf fix 172 | * Compatible with Option Explicit 173 | * JSON escape characters 174 | -------------------------------------------------------------------------------- /aspJSON1.18.asp: -------------------------------------------------------------------------------- 1 | <% 2 | 'August 2020 - Version 1.18 by Gerrit van Kuipers 3 | Class aspJSON 4 | Public data 5 | Private p_JSONstring 6 | private aj_in_string, aj_in_escape, aj_i_tmp, aj_char_tmp, aj_s_tmp, aj_line_tmp, aj_line, aj_lines, aj_currentlevel, aj_currentkey, aj_currentvalue, aj_newlabel, aj_XmlHttp, aj_RegExp, aj_colonfound 7 | 8 | Private Sub Class_Initialize() 9 | Set data = Collection() 10 | 11 | Set aj_RegExp = new regexp 12 | aj_RegExp.Pattern = "\s{0,}(\S{1}[\s,\S]*\S{1})\s{0,}" 13 | aj_RegExp.Global = False 14 | aj_RegExp.IgnoreCase = True 15 | aj_RegExp.Multiline = True 16 | End Sub 17 | 18 | Private Sub Class_Terminate() 19 | Set data = Nothing 20 | Set aj_RegExp = Nothing 21 | End Sub 22 | 23 | Public Sub loadJSON(inputsource) 24 | inputsource = aj_MultilineTrim(inputsource) 25 | If Len(inputsource) = 0 Then Err.Raise 1, "loadJSON Error", "No data to load." 26 | 27 | select case Left(inputsource, 1) 28 | case "{", "[" 29 | case else 30 | Set aj_XmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP") 31 | aj_XmlHttp.open "POST", inputsource, False 32 | aj_XmlHttp.setRequestHeader "Content-Type", "text/json" 33 | aj_XmlHttp.setRequestHeader "CharSet", "UTF-8" 34 | aj_XmlHttp.Send 35 | inputsource = aj_XmlHttp.responseText 36 | set aj_XmlHttp = Nothing 37 | end select 38 | 39 | p_JSONstring = CleanUpJSONstring(inputsource) 40 | aj_lines = Split(p_JSONstring, Chr(13) & Chr(10)) 41 | 42 | Dim level(99) 43 | aj_currentlevel = 1 44 | Set level(aj_currentlevel) = data 45 | For Each aj_line In aj_lines 46 | aj_currentkey = "" 47 | aj_currentvalue = "" 48 | If Instr(aj_line, ":") > 0 Then 49 | aj_in_string = False 50 | aj_in_escape = False 51 | aj_colonfound = False 52 | For aj_i_tmp = 1 To Len(aj_line) 53 | If aj_in_escape Then 54 | aj_in_escape = False 55 | Else 56 | Select Case Mid(aj_line, aj_i_tmp, 1) 57 | Case """" 58 | aj_in_string = Not aj_in_string 59 | Case ":" 60 | If Not aj_in_escape And Not aj_in_string Then 61 | aj_currentkey = Left(aj_line, aj_i_tmp - 1) 62 | aj_currentvalue = Mid(aj_line, aj_i_tmp + 1) 63 | aj_colonfound = True 64 | Exit For 65 | End If 66 | Case "\" 67 | aj_in_escape = True 68 | End Select 69 | End If 70 | Next 71 | if aj_colonfound then 72 | aj_currentkey = aj_Strip(aj_JSONDecode(aj_currentkey), """") 73 | If Not level(aj_currentlevel).exists(aj_currentkey) Then level(aj_currentlevel).Add aj_currentkey, "" 74 | end if 75 | End If 76 | If right(aj_line,1) = "{" Or right(aj_line,1) = "[" Then 77 | If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count 78 | Set level(aj_currentlevel).Item(aj_currentkey) = Collection() 79 | Set level(aj_currentlevel + 1) = level(aj_currentlevel).Item(aj_currentkey) 80 | aj_currentlevel = aj_currentlevel + 1 81 | aj_currentkey = "" 82 | ElseIf right(aj_line,1) = "}" Or right(aj_line,1) = "]" or right(aj_line,2) = "}," Or right(aj_line,2) = "]," Then 83 | aj_currentlevel = aj_currentlevel - 1 84 | ElseIf Len(Trim(aj_line)) > 0 Then 85 | if Len(aj_currentvalue) = 0 Then aj_currentvalue = aj_line 86 | aj_currentvalue = getJSONValue(aj_currentvalue) 87 | 88 | If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count 89 | level(aj_currentlevel).Item(aj_currentkey) = aj_currentvalue 90 | End If 91 | Next 92 | End Sub 93 | 94 | Public Function Collection() 95 | set Collection = Server.CreateObject("Scripting.Dictionary") 96 | End Function 97 | 98 | Public Function AddToCollection(dictobj) 99 | if TypeName(dictobj) <> "Dictionary" then Err.Raise 1, "AddToCollection Error", "Not a collection." 100 | aj_newlabel = dictobj.Count 101 | dictobj.Add aj_newlabel, Collection() 102 | set AddToCollection = dictobj.item(aj_newlabel) 103 | end function 104 | 105 | Private Function CleanUpJSONstring(aj_originalstring) 106 | aj_originalstring = Replace(aj_originalstring, Chr(13) & Chr(10), "") 107 | aj_originalstring = Mid(aj_originalstring, 2, Len(aj_originalstring) - 2) 108 | aj_in_string = False : aj_in_escape = False : aj_s_tmp = "" 109 | For aj_i_tmp = 1 To Len(aj_originalstring) 110 | aj_char_tmp = Mid(aj_originalstring, aj_i_tmp, 1) 111 | If aj_in_escape Then 112 | aj_in_escape = False 113 | aj_s_tmp = aj_s_tmp & aj_char_tmp 114 | Else 115 | Select Case aj_char_tmp 116 | Case "\" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_escape = True 117 | Case """" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_string = Not aj_in_string 118 | Case "{", "[" 119 | aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) 120 | Case "}", "]" 121 | aj_s_tmp = aj_s_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) & aj_char_tmp 122 | Case "," : aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) 123 | Case Else : aj_s_tmp = aj_s_tmp & aj_char_tmp 124 | End Select 125 | End If 126 | Next 127 | 128 | CleanUpJSONstring = "" 129 | aj_s_tmp = split(aj_s_tmp, Chr(13) & Chr(10)) 130 | For Each aj_line_tmp In aj_s_tmp 131 | aj_line_tmp = replace(replace(aj_line_tmp, chr(10), ""), chr(13), "") 132 | CleanUpJSONstring = CleanUpJSONstring & aj_Trim(aj_line_tmp) & Chr(13) & Chr(10) 133 | Next 134 | End Function 135 | 136 | Private Function getJSONValue(ByVal val) 137 | val = Trim(val) 138 | If Left(val,1) = ":" Then val = Mid(val, 2) 139 | If Right(val,1) = "," Then val = Left(val, Len(val) - 1) 140 | val = Trim(val) 141 | 142 | Select Case val 143 | Case "true" : getJSONValue = True 144 | Case "false" : getJSONValue = False 145 | Case "null" : getJSONValue = Null 146 | Case Else 147 | If (Instr(val, """") = 0) Then 148 | If IsNumeric(val) Then 149 | getJSONValue = CDbl(val) 150 | Else 151 | getJSONValue = val 152 | End If 153 | Else 154 | If Left(val,1) = """" Then val = Mid(val, 2) 155 | If Right(val,1) = """" Then val = Left(val, Len(val) - 1) 156 | getJSONValue = aj_JSONDecode(Trim(val)) 157 | End If 158 | End Select 159 | End Function 160 | 161 | Private JSONoutput_level 162 | Public Function JSONoutput() 163 | dim wrap_dicttype, aj_label 164 | JSONoutput_level = 1 165 | wrap_dicttype = "[]" 166 | For Each aj_label In data 167 | If Not aj_IsInt(aj_label) Then wrap_dicttype = "{}" 168 | Next 169 | JSONoutput = Left(wrap_dicttype, 1) & Chr(13) & Chr(10) & GetDict(data) & Right(wrap_dicttype, 1) 170 | End Function 171 | 172 | Private Function GetDict(objDict) 173 | dim aj_item, aj_keyvals, aj_label, aj_dicttype 174 | For Each aj_item In objDict 175 | Select Case TypeName(objDict.Item(aj_item)) 176 | Case "Dictionary" 177 | GetDict = GetDict & Space(JSONoutput_level * 4) 178 | 179 | aj_dicttype = "[]" 180 | For Each aj_label In objDict.Item(aj_item).Keys 181 | If Not aj_IsInt(aj_label) Then aj_dicttype = "{}" 182 | Next 183 | If aj_IsInt(aj_item) Then 184 | GetDict = GetDict & (Left(aj_dicttype,1) & Chr(13) & Chr(10)) 185 | Else 186 | GetDict = GetDict & ("""" & aj_JSONEncode(aj_item) & """" & ": " & Left(aj_dicttype,1) & Chr(13) & Chr(10)) 187 | End If 188 | JSONoutput_level = JSONoutput_level + 1 189 | 190 | aj_keyvals = objDict.Keys 191 | 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)) 192 | Case Else 193 | aj_keyvals = objDict.Keys 194 | 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)) 195 | End Select 196 | Next 197 | End Function 198 | 199 | Private Function aj_IsInt(val) 200 | aj_IsInt = (TypeName(val) = "Integer" Or TypeName(val) = "Long") 201 | End Function 202 | 203 | Private Function GetSubDict(objSubDict) 204 | GetSubDict = GetDict(objSubDict) 205 | JSONoutput_level= JSONoutput_level -1 206 | End Function 207 | 208 | Private Function WriteValue(ByVal val) 209 | Select Case TypeName(val) 210 | Case "Double", "Integer", "Long": WriteValue = replace(val, ",", ".") 211 | Case "Null" : WriteValue = "null" 212 | Case "Boolean" : WriteValue = aj_InlineIf(val, "true", "false") 213 | Case Else : WriteValue = """" & aj_JSONEncode(val) & """" 214 | End Select 215 | End Function 216 | 217 | Private Function aj_JSONEncode(ByVal val) 218 | val = Replace(val, "\", "\\") 219 | val = Replace(val, """", "\""") 220 | 'val = Replace(val, "/", "\/") 221 | val = Replace(val, Chr(8), "\b") 222 | val = Replace(val, Chr(12), "\f") 223 | val = Replace(val, Chr(10), "\n") 224 | val = Replace(val, Chr(13), "\r") 225 | val = Replace(val, Chr(9), "\t") 226 | aj_JSONEncode = Trim(val) 227 | End Function 228 | 229 | Private Function aj_JSONDecode(ByVal val) 230 | val = Replace(val, "\""", """") 231 | val = Replace(val, "\\", "\") 232 | val = Replace(val, "\/", "/") 233 | val = Replace(val, "\b", Chr(8)) 234 | val = Replace(val, "\f", Chr(12)) 235 | val = Replace(val, "\n", Chr(10)) 236 | val = Replace(val, "\r", Chr(13)) 237 | val = Replace(val, "\t", Chr(9)) 238 | aj_JSONDecode = Trim(val) 239 | End Function 240 | 241 | Private Function aj_InlineIf(condition, returntrue, returnfalse) 242 | If condition Then aj_InlineIf = returntrue Else aj_InlineIf = returnfalse 243 | End Function 244 | 245 | Private Function aj_Strip(ByVal val, stripper) 246 | If Left(val, 1) = stripper Then val = Mid(val, 2) 247 | If Right(val, 1) = stripper Then val = Left(val, Len(val) - 1) 248 | aj_Strip = val 249 | End Function 250 | 251 | Private Function aj_MultilineTrim(TextData) 252 | aj_MultilineTrim = aj_RegExp.Replace(TextData, "$1") 253 | End Function 254 | 255 | private function aj_Trim(val) 256 | aj_Trim = Trim(val) 257 | Do While Left(aj_Trim, 1) = Chr(9) : aj_Trim = Mid(aj_Trim, 2) : Loop 258 | Do While Right(aj_Trim, 1) = Chr(9) : aj_Trim = Left(aj_Trim, Len(aj_Trim) - 1) : Loop 259 | aj_Trim = Trim(aj_Trim) 260 | end function 261 | End Class 262 | %> -------------------------------------------------------------------------------- /aspJSON1.19.asp: -------------------------------------------------------------------------------- 1 | <% 2 | 'January 2021 - Version 1.1 by Gerrit van Kuipers 3 | Class aspJSON 4 | Public data 5 | Private p_JSONstring 6 | Private aj_in_string, aj_in_escape, aj_i_tmp, aj_char_tmp, aj_s_tmp, aj_line_tmp, aj_line, aj_lines, aj_currentlevel, aj_currentkey, aj_currentvalue, aj_newlabel, aj_XmlHttp, aj_RegExp, aj_colonfound 7 | 8 | Private Sub Class_Initialize() 9 | Set data = Collection() 10 | 11 | Set aj_RegExp = New regexp 12 | aj_RegExp.Pattern = "\s{0,}(\S{1}[\s,\S]*\S{1})\s{0,}" 13 | aj_RegExp.Global = False 14 | aj_RegExp.IgnoreCase = True 15 | aj_RegExp.Multiline = True 16 | End Sub 17 | 18 | Private Sub Class_Terminate() 19 | Set data = Nothing 20 | Set aj_RegExp = Nothing 21 | End Sub 22 | 23 | Public Sub loadJSON(inputsource) 24 | inputsource = aj_MultilineTrim(inputsource) 25 | If Len(inputsource) = 0 Then Err.Raise 1, "loadJSON Error", "No data to load." 26 | 27 | Select Case Left(inputsource, 1) 28 | Case "{", "[" 29 | Case Else 30 | Set aj_XmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP") 31 | aj_XmlHttp.open "POST", inputsource, False 32 | aj_XmlHttp.setRequestHeader "Content-Type", "text/json" 33 | aj_XmlHttp.setRequestHeader "CharSet", "UTF-8" 34 | aj_XmlHttp.Send 35 | inputsource = aj_XmlHttp.responseText 36 | Set aj_XmlHttp = Nothing 37 | End Select 38 | 39 | p_JSONstring = CleanUpJSONstring(inputsource) 40 | aj_lines = Split(p_JSONstring, Chr(13) & Chr(10)) 41 | 42 | Dim level(99) 43 | aj_currentlevel = 1 44 | Set level(aj_currentlevel) = data 45 | For Each aj_line In aj_lines 46 | aj_currentkey = "" 47 | aj_currentvalue = "" 48 | If Instr(aj_line, ":") > 0 Then 49 | aj_in_string = False 50 | aj_in_escape = False 51 | aj_colonfound = False 52 | For aj_i_tmp = 1 To Len(aj_line) 53 | If aj_in_escape Then 54 | aj_in_escape = False 55 | Else 56 | Select Case Mid(aj_line, aj_i_tmp, 1) 57 | Case """" 58 | aj_in_string = Not aj_in_string 59 | Case ":" 60 | If Not aj_in_escape And Not aj_in_string Then 61 | aj_currentkey = Left(aj_line, aj_i_tmp - 1) 62 | aj_currentvalue = Mid(aj_line, aj_i_tmp + 1) 63 | aj_colonfound = True 64 | Exit For 65 | End If 66 | Case "\" 67 | aj_in_escape = True 68 | End Select 69 | End If 70 | Next 71 | if aj_colonfound then 72 | aj_currentkey = aj_Strip(aj_JSONDecode(aj_currentkey), """") 73 | If Not level(aj_currentlevel).exists(aj_currentkey) Then level(aj_currentlevel).Add aj_currentkey, "" 74 | end if 75 | End If 76 | If right(aj_line,1) = "{" Or right(aj_line,1) = "[" Then 77 | If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count 78 | Set level(aj_currentlevel).Item(aj_currentkey) = Collection() 79 | Set level(aj_currentlevel + 1) = level(aj_currentlevel).Item(aj_currentkey) 80 | aj_currentlevel = aj_currentlevel + 1 81 | aj_currentkey = "" 82 | ElseIf right(aj_line,1) = "}" Or right(aj_line,1) = "]" or right(aj_line,2) = "}," Or right(aj_line,2) = "]," Then 83 | aj_currentlevel = aj_currentlevel - 1 84 | ElseIf Len(Trim(aj_line)) > 0 Then 85 | If Len(aj_currentvalue) = 0 Then aj_currentvalue = aj_line 86 | aj_currentvalue = getJSONValue(aj_currentvalue) 87 | 88 | If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count 89 | level(aj_currentlevel).Item(aj_currentkey) = aj_currentvalue 90 | End If 91 | Next 92 | End Sub 93 | 94 | Public Function Collection() 95 | Set Collection = Server.CreateObject("Scripting.Dictionary") 96 | End Function 97 | 98 | Public Function AddToCollection(dictobj) 99 | If TypeName(dictobj) <> "Dictionary" Then Err.Raise 1, "AddToCollection Error", "Not a collection." 100 | aj_newlabel = dictobj.Count 101 | dictobj.Add aj_newlabel, Collection() 102 | Set AddToCollection = dictobj.item(aj_newlabel) 103 | end function 104 | 105 | Private Function CleanUpJSONstring(aj_originalstring) 106 | aj_originalstring = Replace(aj_originalstring, Chr(13) & Chr(10), "") 107 | aj_originalstring = Mid(aj_originalstring, 2, Len(aj_originalstring) - 2) 108 | aj_in_string = False : aj_in_escape = False : aj_s_tmp = "" 109 | For aj_i_tmp = 1 To Len(aj_originalstring) 110 | aj_char_tmp = Mid(aj_originalstring, aj_i_tmp, 1) 111 | If aj_in_escape Then 112 | aj_in_escape = False 113 | aj_s_tmp = aj_s_tmp & aj_char_tmp 114 | Else 115 | Select Case aj_char_tmp 116 | Case "\" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_escape = True 117 | Case """" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_string = Not aj_in_string 118 | Case "{", "[" 119 | aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) 120 | Case "}", "]" 121 | aj_s_tmp = aj_s_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) & aj_char_tmp 122 | Case "," : aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) 123 | Case Else : aj_s_tmp = aj_s_tmp & aj_char_tmp 124 | End Select 125 | End If 126 | Next 127 | 128 | CleanUpJSONstring = "" 129 | aj_s_tmp = Split(aj_s_tmp, Chr(13) & Chr(10)) 130 | For Each aj_line_tmp In aj_s_tmp 131 | aj_line_tmp = Replace(Replace(aj_line_tmp, Chr(10), ""), Chr(13), "") 132 | CleanUpJSONstring = CleanUpJSONstring & aj_Trim(aj_line_tmp) & Chr(13) & Chr(10) 133 | Next 134 | End Function 135 | 136 | Private Function getJSONValue(ByVal val) 137 | val = Trim(val) 138 | If Left(val,1) = ":" Then val = Mid(val, 2) 139 | If Right(val,1) = "," Then val = Left(val, Len(val) - 1) 140 | val = Trim(val) 141 | 142 | Select Case val 143 | Case "true" : getJSONValue = True 144 | Case "false" : getJSONValue = False 145 | Case "null" : getJSONValue = Null 146 | Case Else 147 | If (Instr(val, """") = 0) Then 148 | If IsNumeric(val) Then 149 | getJSONValue = aj_ReadNumericValue(val) 150 | Else 151 | getJSONValue = val 152 | End If 153 | Else 154 | If Left(val,1) = """" Then val = Mid(val, 2) 155 | If Right(val,1) = """" Then val = Left(val, Len(val) - 1) 156 | getJSONValue = aj_JSONDecode(Trim(val)) 157 | End If 158 | End Select 159 | End Function 160 | 161 | Private JSONoutput_level 162 | Public Function JSONoutput() 163 | Dim wrap_dicttype, aj_label 164 | JSONoutput_level = 1 165 | wrap_dicttype = "[]" 166 | For Each aj_label In data 167 | If Not aj_IsInt(aj_label) Then wrap_dicttype = "{}" 168 | Next 169 | JSONoutput = Left(wrap_dicttype, 1) & Chr(13) & Chr(10) & GetDict(data) & Right(wrap_dicttype, 1) 170 | End Function 171 | 172 | Private Function GetDict(objDict) 173 | Dim aj_item, aj_keyvals, aj_label, aj_dicttype 174 | For Each aj_item In objDict 175 | Select Case TypeName(objDict.Item(aj_item)) 176 | Case "Dictionary" 177 | GetDict = GetDict & Space(JSONoutput_level * 4) 178 | 179 | aj_dicttype = "[]" 180 | For Each aj_label In objDict.Item(aj_item).Keys 181 | If Not aj_IsInt(aj_label) Then aj_dicttype = "{}" 182 | Next 183 | If aj_IsInt(aj_item) Then 184 | GetDict = GetDict & (Left(aj_dicttype,1) & Chr(13) & Chr(10)) 185 | Else 186 | GetDict = GetDict & ("""" & aj_JSONEncode(aj_item) & """" & ": " & Left(aj_dicttype,1) & Chr(13) & Chr(10)) 187 | End If 188 | JSONoutput_level = JSONoutput_level + 1 189 | 190 | aj_keyvals = objDict.Keys 191 | 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)) 192 | Case Else 193 | aj_keyvals = objDict.Keys 194 | 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)) 195 | End Select 196 | Next 197 | End Function 198 | 199 | Private Function aj_IsInt(val) 200 | aj_IsInt = (TypeName(val) = "Integer" Or TypeName(val) = "Long") 201 | End Function 202 | 203 | Private Function GetSubDict(objSubDict) 204 | GetSubDict = GetDict(objSubDict) 205 | JSONoutput_level= JSONoutput_level -1 206 | End Function 207 | 208 | Private Function WriteValue(ByVal val) 209 | Select Case TypeName(val) 210 | Case "Double", "Integer", "Long": WriteValue = replace(val, ",", ".") 211 | Case "Null" : WriteValue = "null" 212 | Case "Boolean" : WriteValue = aj_InlineIf(val, "true", "false") 213 | Case Else : WriteValue = """" & aj_JSONEncode(val) & """" 214 | End Select 215 | End Function 216 | 217 | Private Function aj_JSONEncode(ByVal val) 218 | val = Replace(val, "\", "\\") 219 | val = Replace(val, """", "\""") 220 | 'val = Replace(val, "/", "\/") 221 | val = Replace(val, Chr(8), "\b") 222 | val = Replace(val, Chr(12), "\f") 223 | val = Replace(val, Chr(10), "\n") 224 | val = Replace(val, Chr(13), "\r") 225 | val = Replace(val, Chr(9), "\t") 226 | aj_JSONEncode = Trim(val) 227 | End Function 228 | 229 | Private Function aj_JSONDecode(ByVal val) 230 | val = Replace(val, "\""", """") 231 | val = Replace(val, "\\", "\") 232 | val = Replace(val, "\/", "/") 233 | val = Replace(val, "\b", Chr(8)) 234 | val = Replace(val, "\f", Chr(12)) 235 | val = Replace(val, "\n", Chr(10)) 236 | val = Replace(val, "\r", Chr(13)) 237 | val = Replace(val, "\t", Chr(9)) 238 | aj_JSONDecode = Trim(val) 239 | End Function 240 | 241 | Private Function aj_InlineIf(condition, returntrue, returnfalse) 242 | If condition Then aj_InlineIf = returntrue Else aj_InlineIf = returnfalse 243 | End Function 244 | 245 | Private Function aj_Strip(ByVal val, stripper) 246 | If Left(val, 1) = stripper Then val = Mid(val, 2) 247 | If Right(val, 1) = stripper Then val = Left(val, Len(val) - 1) 248 | aj_Strip = val 249 | End Function 250 | 251 | Private Function aj_MultilineTrim(TextData) 252 | aj_MultilineTrim = aj_RegExp.Replace(TextData, "$1") 253 | End Function 254 | 255 | Private Function aj_Trim(val) 256 | aj_Trim = Trim(val) 257 | Do While Left(aj_Trim, 1) = Chr(9) : aj_Trim = Mid(aj_Trim, 2) : Loop 258 | Do While Right(aj_Trim, 1) = Chr(9) : aj_Trim = Left(aj_Trim, Len(aj_Trim) - 1) : Loop 259 | aj_Trim = Trim(aj_Trim) 260 | End Function 261 | 262 | Private Function aj_ReadNumericValue(ByVal val) 263 | If Instr(val, ".") > 0 Then 264 | numdecimals = Len(val) - Instr(val, ".") 265 | val = Clng(Replace(val, ".", "")) 266 | val = val / (10 ^ numdecimals) 267 | aj_ReadNumericValue = val 268 | Else 269 | aj_ReadNumericValue = Clng(val) 270 | End If 271 | End Function 272 | End Class 273 | %> --------------------------------------------------------------------------------