http://hanhead.tistory.com/entry/escape-JSON-function
<%
'******************************************************************************************
'' @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
%>
'******************************************************************************************
'' @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
%>
'asp' 카테고리의 다른 글
ASP JSON Utility (0) | 2010.08.24 |
---|---|
ajaxed v 1.0 (0) | 2010.08.24 |
formatBytes (0) | 2010.08.24 |
view source &asp coloring (0) | 2010.08.24 |
alert function in asp (0) | 2010.08.24 |