jQuery的1.9.1无法解析包含转义反斜杠JSON [英] jquery 1.9.1 fails to parse JSON that contains escaped backslash
问题描述
问:我怎么可以使用asp.net的JavaScriptSerializer在服务器上创建JSON字符串,将始终与jQuery的1.9.1 parseJSON(工作)法在IE 9
在服务器,我的对象上调用JavascriptSerializer.Serialize()将其转换为一个JSON字符串。对象具有可包含反斜杠属性。所以需要的JavaScriptSerializer的值是这样的:
有\\反斜杠
和逃脱它是这样的:
有\\\\反斜杠
当我运行生成的JSON字符串通过JsonLint它是有效的:
{HasBackslash:有\\\\反斜杠}
而当我通过JsonLint运行此,它抛出一个解析错误:
{HasBackslash:有\\反斜杠}
在IE9 / jQuery的1.9.1完全相反的是可以接受的。的Javascript通过的JavaScriptSerializer在服务器上创建的位与错误的浏览器无法无效字符
VAR HasBackslash = $ .parseJSON({HasBackslash:有\\\\反斜杠}');
不过的Javascript该位工作得很好:
VAR HasBackslash = $ .parseJSON({HasBackslash:有\\反斜杠}');
下面是完整的例子,我使用jQuery 1.9.1和IE 9。
< HTML的xmlns =http://www.w3.org/1999/xhtml>
< HEAD>
<标题>在标题< /标题>
<脚本类型=文/ JavaScript的SRC =jQuery的-1.9.1.js>< / SCRIPT>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
尝试{
VAR NoBackslash = $ .parseJSON({NoBackslash:没有反斜杠}');
$('#NoBackslash)文本('NoBackslash成功解析');
}
赶上(错误){
$('#NoBackslash)文本('错误解析NoBackslash:'+ err.message)。
} 尝试{
VAR HasBackslash = $ .parseJSON({HasBackslash:有\\反斜杠}');
$('#HasBackslash)文本('HasBackslash成功解析');
}
赶上(错误){
$('#HasBackslash)文本('错误分析HasBackslash:'+ err.message)。
}
尝试{
VAR HasEscapedBackslash = $ .parseJSON({HasEscapedBackslash:有\\\\反斜杠}');
$('#HasEscapedBackslash)文本('HasEscapedBackslash成功解析');
}
赶上(错误){
$('#HasEscapedBackslash)文本('错误分析HasEscapedBackslash:'+ err.message)。
} });
< / SCRIPT>
< /头>
<身体GT;
&LT,P n ='NoBackslash'>< / P>
&LT,P n ='HasBackslash'>< / P>
&LT,P n ='HasEscapedBackslash'>< / P>
< /身体GT;< / HTML>
您绘制错误的结论。你正在改变那里的数据进行评估的环境,这就是为什么你看似矛盾的结果。
让我们开始。
{HasBackslash:有\\反斜杠}
这是无效的JSON,因为 \\乙
是一个无效的转义序列。
{HasBackslash:有\\\\反斜杠}
是有效的,因为 \\\\
是一个有效的转义序列。当JSON解析,它会创建字符 \\
。
现在到:
$ parseJSON({HasBackslash:有\\\\反斜杠}');
由于您使用的是JavaScript的字符串,其中 \\
是转义字符为好, \\
被认为是工作是字符串的一部分。刚刚尝试
>的console.log('{HasBackslash:有\\\\反斜杠}');
{HasBackslash:有\\反斜杠}
您看到的,的字符串值的,这是传递给 $。parseJSON
的真正价值,是因为第一次一样,这是无效的。
然而,
$ parseJSON({HasBackslash:有\\反斜杠}');
的作品,因为再次,你用一个JavaScript字符串工作,所以被解析的值为
>的console.log('{HasBackslash:有\\反斜杠}');
{HasBackslash:HasBackslash}
不含反斜杠
所以,只要你不是嵌入生成的JSON到一个JavaScript字符串,你应该罚款。
Question: how can I use the asp.net JavascriptSerializer on the server to create JSON strings that will always work with the jquery 1.9.1 parseJSON() method in IE 9?
On the server I am calling JavascriptSerializer.Serialize() on an object to convert it to a JSON string. The object has properties which can contain a backslash. So the JavascriptSerializer takes a value like this:
Has\Backslash
And escapes it like this:
Has\\Backslash
When I run the resulting JSON string through JsonLint it is validated:
{"HasBackslash":"Has\\Backslash"}
And when I run this through JsonLint it throws a parse error:
{"HasBackslash":"Has\Backslash"}
In IE9/jquery 1.9.1 the exact opposite is acceptable. The bit of Javascript created by JavascriptSerializer on the server fails in the browser with the error "Invalid Character":
var HasBackslash = $.parseJSON('{"HasBackslash":"Has\\Backslash"}');
But this bit of Javascript works just fine:
var HasBackslash = $.parseJSON('{"HasBackslash":"Has\Backslash"}');
Here is full example, I'm using jquery 1.9.1 and IE 9.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Title</title>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
try {
var NoBackslash = $.parseJSON('{"NoBackslash":"No Backslash"}');
$('#NoBackslash').text('NoBackslash parsed successfully');
}
catch(err){
$('#NoBackslash').text('Error parsing NoBackslash: ' + err.message);
}
try {
var HasBackslash = $.parseJSON('{"HasBackslash":"Has\Backslash"}');
$('#HasBackslash').text('HasBackslash parsed successfully');
}
catch (err) {
$('#HasBackslash').text('Error parsing HasBackslash: ' + err.message);
}
try {
var HasEscapedBackslash = $.parseJSON('{"HasEscapedBackslash":"Has\\Backslash"}');
$('#HasEscapedBackslash').text('HasEscapedBackslash parsed successfully');
}
catch(err){
$('#HasEscapedBackslash').text('Error parsing HasEscapedBackslash: ' + err.message);
}
});
</script>
</head>
<body>
<p id='NoBackslash'></p>
<p id='HasBackslash'></p>
<p id='HasEscapedBackslash'></p>
</body></html>
You are drawing the wrong conclusions. You are changing the environment where the data is evaluated and that's why you get seemingly contradicting results.
Lets start with.
{"HasBackslash":"Has\Backslash"}
It's invalid JSON because \B
is an invalid escape sequence.
{"HasBackslash":"Has\\Backslash"}
is valid because \\
is a valid escape sequence. When the JSON is parsed, it will create the character \
.
Now to:
$.parseJSON('{"HasBackslash":"Has\\Backslash"}');
Since you are working with a JavaScript string where \
is the escape character as well, the \
are considered to be part of the string literal. Just try
> console.log('{"HasBackslash":"Has\\Backslash"}');
{"HasBackslash":"Has\Backslash"}
You see, the string value, which is the real value that is passed to $.parseJSON
, is the same as first one, which was invalid.
However,
$.parseJSON('{"HasBackslash":"Has\Backslash"}');
works, since again, you are working with a JavaScript string, so the value being parsed is
> console.log('{"HasBackslash":"Has\Backslash"}');
{"HasBackslash":"HasBackslash"}
which does not contain a backslash.
So, as long as you are not embedding the generated JSON into a JavaScript string, you should be fine.
这篇关于jQuery的1.9.1无法解析包含转义反斜杠JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!