网络上流传的 vbsEscape UnEscape都只针对asp而言,js传递给asp时可进行转义解码,但asp输出json时如果用这些函数进行编码的话,js是无法正常解码的。
下面是经过实际验证可用的asp escape函数:
原文地址:http://www.webdevbros.net/2007/04/26/json-character-escaping-function-in-classic-asp/
'******************************************************************************************
'' @SDESCRIPTION: takes a given string and makes it JSON valid (http://json.org/)
'' @AUTHOR: Michael Rebec
'' @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
'******************************************************************************************
public function escapeJSON(val)
cDoubleQuote = &h22
cRevSolidus = &h5C
cSolidus = &h2F
for i = 1 to (len(val))
currentDigit = mid(val, i, 1)
if asc(currentDigit) > &h00 and asc(currentDigit) < &h1F then
currentDigit = escapeJSONSquence(currentDigit)
elseif asc(currentDigit) >= &hC280 and asc(currentDigit) <= &hC2BF then
currentDigit = "u00" + right(padLeft(hex(asc(currentDigit) - &hC200), 2, 0), 2)
elseif asc(currentDigit) >= &hC380 and asc(currentDigit) <= &hC3BF then
currentDigit = "u00" + right(padLeft(hex(asc(currentDigit) - &hC2C0), 2, 0), 2)
else
select case asc(currentDigit)
case cDoubleQuote: currentDigit = escapeJSONSquence(currentDigit)
case cRevSolidus: currentDigit = escapeJSONSquence(currentDigit)
case cSolidus: currentDigit = escapeJSONSquence(currentDigit)
end select
end if
escapeJSON = escapeJSON & currentDigit
next
end function
function escapeJSONSquence(digit)
escapeJSONSquence = "u00" + right(padLeft(hex(asc(digit)), 2, 0), 2)
end function
function padLeft(value, totalLength, paddingChar)
padLeft = right(clone(paddingChar, totalLength) & value, totalLength)
end function
public function clone(byVal str, n)
for i = 1 to n : clone = clone & str : next
end function