打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
asp返回json数据 json.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<%
'**********************************************************************************************
'* GAB_LIBRARY Copyright (C) 2003 - This file is part of GAB_LIBRARY
'* For license refer to the license.txt
'***********************************************************************************************
'****************************************************************************************
'' @CLASSTITLE:        JSON
'' @CREATOR:        Michal Gabrukiewicz (gabru at grafix.at), Michael Rebec
'' @CONTRIBUTORS:    - Cliff Pruitt (opensource at crayoncowboy.com)
''                    - Sylvain Lafontaine
'' @CREATEDON:        2007-04-26 12:46
'' @CDESCRIPTION:    Comes up with functionality for JSON (http://json.org) to use within ASP.
''                     Correct escaping of characters, generating JSON Grammer out of ASP datatypes and structures
'' @REQUIRES:        -
'' @OPTIONEXPLICIT:    yes
'' @VERSION:        1.4
'****************************************************************************************
class JSON
'private members
private output, innerCall
'public members
public toResponse        ''[bool] should generated results be directly written to the response? default = false
'*********************************************************************************
'* constructor
'*********************************************************************************
public sub class_initialize()
newGeneration()
toResponse = false
end sub
'******************************************************************************************
'' @SDESCRIPTION:    STATIC! takes a given string and makes it JSON valid
'' @DESCRIPTION:    all characters which needs to be escaped are beeing replaced by their
''                    unicode representation according to the
''                    RFC4627#2.5 - http://www.ietf.org/rfc/rfc4627.txt?number=4627
'' @PARAM:            val [string]: value which should be escaped
'' @RETURN:            [string] JSON valid string
'' asc 函数被替换成ascw函数以便支持中文
'******************************************************************************************
public function escape(val)
dim cDoubleQuote, cRevSolidus, cSolidus
cDoubleQuote = &h22
cRevSolidus = &h5C
cSolidus = &h2F
dim i, currentDigit
for i = 1 to (len(val))
currentDigit = mid(val, i, 1)
if ascw(currentDigit) > &h00 and ascw(currentDigit) < &h1F then
currentDigit = escapequence(currentDigit)
elseif ascw(currentDigit) >= &hC280 and ascw(currentDigit) <= &hC2BF then
currentDigit = "\u00" + right(padLeft(hex(asc(currentDigit) - &hC200), 2, 0), 2)
elseif ascw(currentDigit) >= &hC380 and ascw(currentDigit) <= &hC3BF then
currentDigit = "\u00" + right(padLeft(hex(ascw(currentDigit) - &hC2C0), 2, 0), 2)
else
select case ascw(currentDigit)
case cDoubleQuote: currentDigit = escapequence(currentDigit)
case cRevSolidus: currentDigit = escapequence(currentDigit)
case cSolidus: currentDigit = escapequence(currentDigit)
end select
end if
escape = escape & currentDigit
next
end function
'******************************************************************************
'' @SDESCRIPTION:    generates a representation of a name value pair in JSON grammer
'' @DESCRIPTION:    the generation is done fully recursive so the value can be a complex datatype as well. e.g.
''                    toJSON("n", array(array(), 2, true), false) or toJSON("n", array(RS, dict, false), false), etc.
'' @PARAM:            name [string]: name of the value (accessible with
javascript afterwards). leave empty to get just the value
'' @PARAM:            val [variant], [int], [float], [array], [object], [dictionary], [recordset]: value which needs
''                    to be generated. Conversation of the data types (ASP datatype -> Javascript datatype):
''                    NOTHING, NULL -> null, ARRAY -> array, BOOL -> bool, OBJECT -> name of the type,
''                    MULTIDIMENSIONAL ARRAY -> generates a 1 dimensional array (flat) with all values of the multidim array
''                    DICTIONARY -> valuepairs. each key is accessible as property afterwards
''                    RECORDSET -> array where each row of the recordset represents a field in the array.
''                    fields have properties named after the column names of the recordset (LOWERCASED!)
''                    e.g. generate(RS) can be used afterwards r[0].ID
''                    INT, FLOAT -> number
''                    OBJECT with reflect() method -> returned as object which can be used within JavaScript
'' @PARAM:            nested [bool]: is the value pair already nested within another? if yes then the {} are left out.
'' @RETURN:            [string] returns a JSON representation of the given name value pair
''                    (if toResponse is on then the return is written directly to the response and nothing is returned)
'*******************************************************************************************
public function toJSON(name, val, nested)
if not nested and not isEmpty(name) then write("{")
if not isEmpty(name) then write("""" & escape(name) & """: ")
generateValue(val)
if not nested and not isEmpty(name) then write("}")
toJSON = output
if innerCall = 0 then newGeneration()
end function
'*********************************************************************************
'* generate
'******************************************************************************
private function generateValue(val)
if isNull(val) then
write("null")
elseif isArray(val) then
generateArray(val)
elseif isObject(val) then
if val is nothing then
write("null")
elseif typename(val) = "Dictionary" then
generateDictionary(val)
elseif typename(val) = "Recordset" then
generateRecordset(val)
else
generateObject(val)
end if
else
'bool
varTyp = varType(val)
if varTyp = 11 then
if val then write("true") else write("false")
'int, long, byte
elseif varTyp = 2 or varTyp = 3 or varTyp = 17 or varTyp = 19 then
write(cLng(val))
'single, double, currency
elseif varTyp = 4 or varTyp = 5 or varTyp = 6 or varTyp = 14 then
write(replace(cDbl(val), ",", "."))
else
write("""" & escape(val & "") & """")
end if
end if
generateValue = output
end function
'*****************************************************************************
'* generateArray
'*****************************************************************************
private sub generateArray(val)
dim item, i
write("[")
i = 0
'the for each allows us to support also multi dimensional arrays
for each item in val
if i > 0 then write(",")
generateValue(item)
i = i + 1
next
write("]")
end sub
'*********************************************************************************
'* generateDictionary
'**************************************************************************
private sub generateDictionary(val)
dim keys, i
innerCall = innerCall + 1
write("{")
keys = val.keys
for i = 0 to uBound(keys)
if i > 0 then write(",")
toJSON keys(i), val(keys(i)), true
next
write("}")
innerCall = innerCall - 1
end sub
'*******************************************************************
'* generateRecordset
'*******************************************************************
private sub generateRecordset(val)
dim i
write("[")
while not val.eof
innerCall = innerCall + 1
write("{")
for i = 0 to val.fields.count - 1
if i > 0 then write(",")
toJSON lCase(val.fields(i).name), val.fields(i).value, true
next
write("}")
val.movenext()
if not val.eof then write(",")
innerCall = innerCall - 1
wend
write("]")
end sub
'*******************************************************************************
'* generateObject
'*******************************************************************************
private sub generateObject(val)
dim props
on error resume next
set props = val.reflect()
if err = 0 then
on error goto 0
innerCall = innerCall + 1
toJSON empty, props, true
innerCall = innerCall - 1
else
on error goto 0
write("""" & escape(typename(val)) & """")
end if
end sub
'*******************************************************************************
'* newGeneration
'*******************************************************************************
private sub newGeneration()
output = empty
innerCall = 0
end sub
'*******************************************************************************
'* JsonEscapeSquence
'*******************************************************************************
private function escapequence(digit)
escapequence = "\u00" + right(padLeft(hex(asc(digit)), 2, 0), 2)
end function
'*****************************************************************************
'* padLeft
'*****************************************************************************
private function padLeft(value, totalLength, paddingChar)
padLeft = right(clone(paddingChar, totalLength) & value, totalLength)
end function
'*****************************************************************************
'* clone
'******************************************************************************************
public function clone(byVal str, n)
dim i
for i = 1 to n : clone = clone & str : next
end function
'******************************************************************************************
'* write
'******************************************************************************************
private sub write(val)
if toResponse then
htm = htm&(val)
else
output = output & val
end if
end sub
end class
%>
好消息! 现在订阅阿飞博客,立即赠送最新最好最有价值的资源给你!
第一步:点击这里
第二步:输入您的QQ邮箱
第三步:登陆QQ邮箱,点击确认连接
第四步:成功订阅
如果感觉本文章对你有价值,可按您心情自愿付费: 支付宝 andyzeng981@163.com 曾德飞
除非注明,文章均为(阿飞博客 )原创,转载请保留链接:http://zengdefei.com/551.html
订阅公众号:diqiujiayuancom 站长微信(QQ):81531444
Loading...
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
json.asp
php语言的json实现
多维数组与Json格式的转化
ecshop在dwt模板中和lbi中输入数组详情的方法 ecshop模板中输出数组的方法
计算机技术基础(第五章 选择结构程序设计 )
VB什么情况下inputbox要用val
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服