ASP 下 能作为json输出后js能解密的 escape 函数

摘要:
网络上流传的vbsscape UnEscape仅适用于asp。当js传递给asp时,可以对其进行转义和解码,但当asp输出json时,如果这些函数用于编码,则无法正常解码js。经过实际验证,以下asp转义函数可用:原始地址:http://www.webdevbros.net/2007/04/26/json-character-escaping-function-in-classic-a
网络上流传的 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

免责声明:文章转载自《ASP 下 能作为json输出后js能解密的 escape 函数》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇手电筒查询bat批量备份手机APP下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

js运算符!

算术运算符(如下) 1.“+” 加 2.“-” 减 3“*” 乘 4.“/” 除 5.“%” 取余 例:var i=3,j=8; document.write(i+j); 加 document.write(i-j); 减 document.write(i*j); 乘 document.write(i/j); 除 document.write(i%j); 取...

自定义JS控件-简单示例

1、  业务需求: 制作 一个按钮对象,然后 像 winfrom  那样调用 就可以了: 首先 我们新建一个 MyControls的 JS文件:(插入如下代码) //这里运用的面向对象的思想 ,新建了一个按钮对象 var button = function (ClientId) { this.control = null;...

使用js设置input标签只读 readonly 属性

先上代码: <html> <head> <title> test </title> <meta charset="utf-8"> </head> <script language="JavaScript"> function setReadon...

Js/如何修改easyui修饰的input的val值

1.关于js对input值的修改介绍:一般js改变input的val值,我一直使用的方法是: $('#id').val('test');这样的方式来进行修改。但是我使 用了class="easyui-textbox"来对input进行修饰。使用上面的方法就不起作用。 这里改为:$('#id').textbox('setValue', '');...

repeater简单应用(嵌套绑定)

眼看就要到上班的日子了,希望大家抓住宝贵时间能吃则吃,能乐则乐 咱还是节省网络资源,进入正题。。 说起repeater嵌套,好像从N久就有了,这个例子是前些日子做的,用了三层嵌套实现有,先说下,因为客观需求,并没有考虑到性能上的问题,所以可以看下其中的方法,至于别的可以自己探索下,呵呵。 功能是要实现一个下拉列表,列表顶层是可以动态设置的年份时间段,第二层...

前端-Vue基础1

Vue核心思想:只要改变数据,页面就会发生改变 1.引入vue 1.下载vue.js 2.在script标签的src属性中,引入vue.js <script src="js/vue.js"></script> 2.vue实例 el:vue接管的div元素,注:只能接管一个div,所有需要vue处理的,必须写在这个div中 data...