Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

281 строка
9.4KB

  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. Private Sub Class_Initialize()
  8. Set data = Collection()
  9. Set aj_RegExp = New regexp
  10. aj_RegExp.Pattern = "\s{0,}(\S{1}[\s,\S]*\S{1})\s{0,}"
  11. aj_RegExp.Global = False
  12. aj_RegExp.IgnoreCase = True
  13. aj_RegExp.Multiline = True
  14. End Sub
  15. Private Sub Class_Terminate()
  16. Set data = Nothing
  17. Set aj_RegExp = Nothing
  18. End Sub
  19. Public Sub loadJSON(inputsource)
  20. inputsource = aj_MultilineTrim(inputsource)
  21. If Len(inputsource) = 0 Then Err.Raise 1, "loadJSON Error", "No data to load."
  22. Select Case Left(inputsource, 1)
  23. Case "{", "["
  24. Case Else
  25. Set aj_XmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
  26. aj_XmlHttp.open "POST", inputsource, False
  27. aj_XmlHttp.setRequestHeader "Content-Type", "text/json"
  28. aj_XmlHttp.setRequestHeader "CharSet", "UTF-8"
  29. aj_XmlHttp.Send
  30. inputsource = aj_XmlHttp.responseText
  31. Set aj_XmlHttp = Nothing
  32. End Select
  33. p_JSONstring = CleanUpJSONstring(inputsource)
  34. aj_lines = Split(p_JSONstring, Chr(13) & Chr(10))
  35. Dim level(99)
  36. aj_currentlevel = 1
  37. Set level(aj_currentlevel) = data
  38. For Each aj_line In aj_lines
  39. aj_currentkey = ""
  40. aj_currentvalue = ""
  41. If Instr(aj_line, ":") > 0 Then
  42. aj_in_string = False
  43. aj_in_escape = False
  44. aj_colonfound = False
  45. For aj_i_tmp = 1 To Len(aj_line)
  46. If aj_in_escape Then
  47. aj_in_escape = False
  48. Else
  49. Select Case Mid(aj_line, aj_i_tmp, 1)
  50. Case """"
  51. aj_in_string = Not aj_in_string
  52. Case ":"
  53. If Not aj_in_escape And Not aj_in_string Then
  54. aj_currentkey = Left(aj_line, aj_i_tmp - 1)
  55. aj_currentvalue = Mid(aj_line, aj_i_tmp + 1)
  56. aj_colonfound = True
  57. Exit For
  58. End If
  59. Case "\"
  60. aj_in_escape = True
  61. End Select
  62. End If
  63. Next
  64. if aj_colonfound then
  65. aj_currentkey = aj_Strip(aj_JSONDecode(aj_currentkey), """")
  66. If Not level(aj_currentlevel).exists(aj_currentkey) Then level(aj_currentlevel).Add aj_currentkey, ""
  67. end if
  68. End If
  69. If right(aj_line,1) = "{" Or right(aj_line,1) = "[" Then
  70. If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
  71. Set level(aj_currentlevel).Item(aj_currentkey) = Collection()
  72. Set level(aj_currentlevel + 1) = level(aj_currentlevel).Item(aj_currentkey)
  73. aj_currentlevel = aj_currentlevel + 1
  74. aj_currentkey = ""
  75. ElseIf right(aj_line,1) = "}" Or right(aj_line,1) = "]" or right(aj_line,2) = "}," Or right(aj_line,2) = "]," Then
  76. aj_currentlevel = aj_currentlevel - 1
  77. ElseIf Len(Trim(aj_line)) > 0 Then
  78. If Len(aj_currentvalue) = 0 Then aj_currentvalue = aj_line
  79. aj_currentvalue = getJSONValue(aj_currentvalue)
  80. If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
  81. level(aj_currentlevel).Item(aj_currentkey) = aj_currentvalue
  82. End If
  83. Next
  84. End Sub
  85. Public Function Collection()
  86. Set Collection = Server.CreateObject("Scripting.Dictionary")
  87. End Function
  88. Public Function AddToCollection(dictobj)
  89. If TypeName(dictobj) <> "Dictionary" Then Err.Raise 1, "AddToCollection Error", "Not a collection."
  90. aj_newlabel = dictobj.Count
  91. dictobj.Add aj_newlabel, Collection()
  92. Set AddToCollection = dictobj.item(aj_newlabel)
  93. end function
  94. Private Function CleanUpJSONstring(aj_originalstring)
  95. aj_originalstring = Replace(aj_originalstring, Chr(13) & Chr(10), "")
  96. aj_originalstring = Mid(aj_originalstring, 2, Len(aj_originalstring) - 2)
  97. aj_in_string = False : aj_in_escape = False : aj_s_tmp = ""
  98. For aj_i_tmp = 1 To Len(aj_originalstring)
  99. aj_char_tmp = Mid(aj_originalstring, aj_i_tmp, 1)
  100. If aj_in_escape Then
  101. aj_in_escape = False
  102. aj_s_tmp = aj_s_tmp & aj_char_tmp
  103. Else
  104. Select Case aj_char_tmp
  105. Case "\" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_escape = True
  106. Case """" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_string = Not aj_in_string
  107. Case "{", "["
  108. aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))
  109. Case "}", "]"
  110. aj_s_tmp = aj_s_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) & aj_char_tmp
  111. Case "," : aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))
  112. Case Else : aj_s_tmp = aj_s_tmp & aj_char_tmp
  113. End Select
  114. End If
  115. Next
  116. CleanUpJSONstring = ""
  117. aj_s_tmp = Split(aj_s_tmp, Chr(13) & Chr(10))
  118. For Each aj_line_tmp In aj_s_tmp
  119. aj_line_tmp = Replace(Replace(aj_line_tmp, Chr(10), ""), Chr(13), "")
  120. CleanUpJSONstring = CleanUpJSONstring & aj_Trim(aj_line_tmp) & Chr(13) & Chr(10)
  121. Next
  122. End Function
  123. Private Function getJSONValue(ByVal val)
  124. val = Trim(val)
  125. If Left(val,1) = ":" Then val = Mid(val, 2)
  126. If Right(val,1) = "," Then val = Left(val, Len(val) - 1)
  127. val = Trim(val)
  128. Select Case val
  129. Case "true" : getJSONValue = True
  130. Case "false" : getJSONValue = False
  131. Case "null" : getJSONValue = Null
  132. Case Else
  133. If (Instr(val, """") = 0) Then
  134. If IsNumeric(val) Then
  135. getJSONValue = aj_ReadNumericValue(val)
  136. Else
  137. getJSONValue = val
  138. End If
  139. Else
  140. If Left(val,1) = """" Then val = Mid(val, 2)
  141. If Right(val,1) = """" Then val = Left(val, Len(val) - 1)
  142. getJSONValue = aj_JSONDecode(Trim(val))
  143. End If
  144. End Select
  145. End Function
  146. Private JSONoutput_level
  147. Public Function JSONoutput()
  148. Dim wrap_dicttype, aj_label
  149. JSONoutput_level = 1
  150. wrap_dicttype = "[]"
  151. For Each aj_label In data
  152. If Not aj_IsInt(aj_label) Then wrap_dicttype = "{}"
  153. Next
  154. JSONoutput = Left(wrap_dicttype, 1) & Chr(13) & Chr(10) & GetDict(data) & Right(wrap_dicttype, 1)
  155. End Function
  156. Private Function GetDict(objDict)
  157. Dim aj_item, aj_keyvals, aj_label, aj_dicttype
  158. For Each aj_item In objDict
  159. Select Case TypeName(objDict.Item(aj_item))
  160. Case "Dictionary"
  161. GetDict = GetDict & Space(JSONoutput_level * 4)
  162. aj_dicttype = "[]"
  163. For Each aj_label In objDict.Item(aj_item).Keys
  164. If Not aj_IsInt(aj_label) Then aj_dicttype = "{}"
  165. Next
  166. If aj_IsInt(aj_item) Then
  167. GetDict = GetDict & (Left(aj_dicttype,1) & Chr(13) & Chr(10))
  168. Else
  169. GetDict = GetDict & ("""" & aj_JSONEncode(aj_item) & """" & ": " & Left(aj_dicttype,1) & Chr(13) & Chr(10))
  170. End If
  171. JSONoutput_level = JSONoutput_level + 1
  172. aj_keyvals = objDict.Keys
  173. 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))
  174. Case Else
  175. aj_keyvals = objDict.Keys
  176. 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))
  177. End Select
  178. Next
  179. End Function
  180. Private Function aj_IsInt(val)
  181. aj_IsInt = (TypeName(val) = "Integer" Or TypeName(val) = "Long")
  182. End Function
  183. Private Function GetSubDict(objSubDict)
  184. GetSubDict = GetDict(objSubDict)
  185. JSONoutput_level= JSONoutput_level -1
  186. End Function
  187. Private Function WriteValue(ByVal val)
  188. Select Case TypeName(val)
  189. Case "Double", "Integer", "Long": WriteValue = replace(val, ",", ".")
  190. Case "Null" : WriteValue = "null"
  191. Case "Boolean" : WriteValue = aj_InlineIf(val, "true", "false")
  192. Case Else : WriteValue = """" & aj_JSONEncode(val) & """"
  193. End Select
  194. End Function
  195. Private Function aj_JSONEncode(ByVal val)
  196. val = Replace(val, "\", "\\")
  197. val = Replace(val, """", "\""")
  198. 'val = Replace(val, "/", "\/")
  199. val = Replace(val, Chr(8), "\b")
  200. val = Replace(val, Chr(12), "\f")
  201. val = Replace(val, Chr(10), "\n")
  202. val = Replace(val, Chr(13), "\r")
  203. val = Replace(val, Chr(9), "\t")
  204. aj_JSONEncode = Trim(val)
  205. End Function
  206. Private Function aj_JSONDecode(ByVal val)
  207. val = Replace(val, "\""", """")
  208. val = Replace(val, "\\", "\")
  209. val = Replace(val, "\/", "/")
  210. val = Replace(val, "\b", Chr(8))
  211. val = Replace(val, "\f", Chr(12))
  212. val = Replace(val, "\n", Chr(10))
  213. val = Replace(val, "\r", Chr(13))
  214. val = Replace(val, "\t", Chr(9))
  215. aj_JSONDecode = Trim(val)
  216. End Function
  217. Private Function aj_InlineIf(condition, returntrue, returnfalse)
  218. If condition Then aj_InlineIf = returntrue Else aj_InlineIf = returnfalse
  219. End Function
  220. Private Function aj_Strip(ByVal val, stripper)
  221. If Left(val, 1) = stripper Then val = Mid(val, 2)
  222. If Right(val, 1) = stripper Then val = Left(val, Len(val) - 1)
  223. aj_Strip = val
  224. End Function
  225. Private Function aj_MultilineTrim(TextData)
  226. aj_MultilineTrim = aj_RegExp.Replace(TextData, "$1")
  227. End Function
  228. Private Function aj_Trim(val)
  229. aj_Trim = Trim(val)
  230. Do While Left(aj_Trim, 1) = Chr(9) : aj_Trim = Mid(aj_Trim, 2) : Loop
  231. Do While Right(aj_Trim, 1) = Chr(9) : aj_Trim = Left(aj_Trim, Len(aj_Trim) - 1) : Loop
  232. aj_Trim = Trim(aj_Trim)
  233. End Function
  234. Private Function aj_ReadNumericValue(ByVal val)
  235. If Instr(val, ".") > 0 Then
  236. numdecimals = Len(val) - Instr(val, ".")
  237. val = Clng(Replace(val, ".", ""))
  238. val = val / (10 ^ numdecimals)
  239. aj_ReadNumericValue = val
  240. Else
  241. aj_ReadNumericValue = Clng(val)
  242. End If
  243. End Function
  244. End Class
  245. dim json_Class__Singleton
  246. Function json()
  247. if IsEmpty(json_Class__Singleton) then
  248. set json_Class__Singleton = new aspJSON
  249. end if
  250. set json = json_Class__Singleton
  251. End Function
  252. %>

Powered by TurnKey Linux.