Discuz教程网

ASP JSON类源码分享

[复制链接]
authicon dly 发表于 2011-9-7 12:34:52 | 显示全部楼层 |阅读模式

  1. <%
  2. '============================================================
  3. ' 文件名称 : /Cls_Json.asp
  4. ' 文件作用 : 系统JSON类文件
  5. ' 文件版本 : VBS JSON(JavaScript Object Notation) Version 2.0.2
  6. ' 程序修改 : Cloud.L
  7. ' 最后更新 : 2009-05-12
  8. '============================================================
  9. ' 程序核心 : JSON官方 [url]http://www.json.org/[/url]
  10. ' 作者博客 : [url]Http://www.cnode.cn[/url]
  11. '============================================================
  12. Class Json_Cls

  13. Public Collection
  14. Public Count
  15. Public QuotedVars '是否为变量增加引号
  16. Public Kind ' 0 = object, 1 = array

  17. Private Sub Class_Initialize
  18. Set Collection = Server.CreateObject(GP_ScriptingDictionary)
  19. QuotedVars = True
  20. Count = 0
  21. End Sub

  22. Private Sub Class_Terminate
  23. Set Collection = Nothing
  24. End Sub

  25. ' counter
  26. Private Property Get Counter
  27. Counter = Count
  28. Count = Count + 1
  29. End Property

  30. ' 设置对象类型
  31. Public Property Let SetKind(ByVal fpKind)
  32. Select Case LCase(fpKind)
  33. Case "object":Kind=0
  34. Case "array":Kind=1
  35. End Select
  36. End Property

  37. ' - data maluplation
  38. ' -- pair
  39. Public Property Let Pair(p, v)
  40. If IsNull(p) Then p = Counter
  41. Collection(p) = v
  42. End Property

  43. Public Property Set Pair(p, v)
  44. If IsNull(p) Then p = Counter
  45. If TypeName(v) <> "Json_Cls" Then
  46. Err.Raise &hD, "class: class", "class object: '" & TypeName(v) & "'"
  47. End If
  48. Set Collection(p) = v
  49. End Property

  50. Public Default Property Get Pair(p)
  51. If IsNull(p) Then p = Count - 1
  52. If IsObject(Collection(p)) Then
  53. Set Pair = Collection(p)
  54. Else
  55. Pair = Collection(p)
  56. End If
  57. End Property
  58. ' -- pair
  59. Public Sub Clean
  60. Collection.RemoveAll
  61. End Sub

  62. Public Sub Remove(vProp)
  63. Collection.Remove vProp
  64. End Sub
  65. ' data maluplation

  66. ' encoding
  67. Public Function jsEncode(str)
  68. Dim i, j, aL1, aL2, c, p

  69. aL1 = Array(&h22, &h5C, &h2F, &h08, &h0C, &h0A, &h0D, &h09)
  70. aL2 = Array(&h22, &h5C, &h2F, &h62, &h66, &h6E, &h72, &h74)
  71. For i = 1 To Len(str)
  72. p = True
  73. c = Mid(str, i, 1)
  74. For j = 0 To 7
  75. If c = Chr(aL1(j)) Then
  76. jsEncode = jsEncode & "" & Chr(aL2(j))
  77. p = False
  78. Exit For
  79. End If
  80. Next

  81. If p Then
  82. Dim a
  83. a = AscW(c)
  84. If a > 31 And a < 127 Then
  85. jsEncode = jsEncode & c
  86. ElseIf a > -1 Or a < 65535 Then
  87. jsEncode = jsEncode & "\u" & String(4 - Len(Hex(a)), "0") & Hex(a)
  88. End If
  89. End If
  90. Next
  91. End Function

  92. ' converting
  93. Public Function toJSON(vPair)
  94. Select Case VarType(vPair)
  95. Case 1 ' Null
  96. toJSON = "null"
  97. Case 7 ' Date
  98. ' yaz saati problemi var
  99. ' jsValue = "new Date(" & Round((vVal - #01/01/1970 02:00#) * 86400000) & ")"
  100. toJSON = """" & CStr(vPair) & """"
  101. Case 8 ' String
  102. toJSON = """" & jsEncode(vPair) & """"
  103. Case 9 ' Object
  104. Dim bFI,i
  105. bFI = True
  106. If vPair.Kind Then toJSON = toJSON & "[" Else toJSON = toJSON & "{"
  107. For Each i In vPair.Collection
  108. If bFI Then bFI = False Else toJSON = toJSON & ","

  109. If vPair.Kind Then
  110. toJSON = toJSON & toJSON(vPair(i))
  111. Else
  112. If QuotedVars Then
  113. toJSON = toJSON & """" & i & """:" & toJSON(vPair(i))
  114. Else
  115. toJSON = toJSON & i & ":" & toJSON(vPair(i))
  116. End If
  117. End If
  118. Next
  119. If vPair.Kind Then toJSON = toJSON & "]" Else toJSON = toJSON & "}"
  120. Case 11
  121. If vPair Then toJSON = "true" Else toJSON = "false"
  122. Case 12, 8192, 8204
  123. Dim sEB
  124. toJSON = MultiArray(vPair, 1, "", sEB)
  125. Case Else
  126. toJSON = Replace(vPair, ",", ".")
  127. End select
  128. End Function

  129. Public Function MultiArray(aBD, iBC, sPS, ByRef sPT) ' Array BoDy, Integer BaseCount, String PoSition
  130. Dim iDU, iDL, i ' Integer DimensionUBound, Integer DimensionLBound
  131. On Error Resume Next
  132. iDL = LBound(aBD, iBC)
  133. iDU = UBound(aBD, iBC)

  134. Dim sPB1, sPB2 ' String PointBuffer1, String PointBuffer2
  135. If Err = 9 Then
  136. sPB1 = sPT & sPS
  137. For i = 1 To Len(sPB1)
  138. If i <> 1 Then sPB2 = sPB2 & ","
  139. sPB2 = sPB2 & Mid(sPB1, i, 1)
  140. Next
  141. MultiArray = MultiArray & toJSON(Eval("aBD(" & sPB2 & ")"))
  142. Else
  143. sPT = sPT & sPS
  144. MultiArray = MultiArray & "["
  145. For i = iDL To iDU
  146. MultiArray = MultiArray & MultiArray(aBD, iBC + 1, i, sPT)
  147. If i < iDU Then MultiArray = MultiArray & ","
  148. Next
  149. MultiArray = MultiArray & "]"
  150. sPT = Left(sPT, iBC - 2)
  151. End If
  152. End Function

  153. Public Property Get ToString
  154. ToString = toJSON(Me)
  155. End Property

  156. Public Sub Flush
  157. If TypeName(Response) <> "Empty" Then
  158. Response.Write(ToString)
  159. ElseIf WScript <> Empty Then
  160. WScript.Echo(ToString)
  161. End If
  162. End Sub

  163. Public Function Clone
  164. Set Clone = ColClone(Me)
  165. End Function

  166. Private Function ColClone(core)
  167. Dim jsc, i
  168. Set jsc = New Json_Cls
  169. jsc.Kind = core.Kind
  170. For Each i In core.Collection
  171. If IsObject(core(i)) Then
  172. Set jsc(i) = ColClone(core(i))
  173. Else
  174. jsc(i) = core(i)
  175. End If
  176. Next
  177. Set ColClone = jsc
  178. End Function

  179. Public Function QueryToJSON(dbc, sql)
  180. Dim rs, jsa,col
  181. Set rs = dbc.Execute(sql)
  182. Set jsa = New Json_Cls
  183. jsa.SetKind="array"
  184. While Not (rs.EOF Or rs.BOF)
  185. Set jsa(Null) = New Json_Cls
  186. jsa(Null).SetKind="object"
  187. For Each col In rs.Fields
  188. jsa(Null)(col.Name) = col.Value
  189. Next
  190. rs.MoveNext
  191. Wend
  192. Set QueryToJSON = jsa
  193. End Function

  194. End Class
  195. %>
复制代码




上一篇:从一个网站扒下的asp生成静态页面的代码 特供版
下一篇:ASP 使用Filter函数来检索数组的实现代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 09:50

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表