json.asp 8.9 KB

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