javascript JSON 响应中的非法字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10020226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Illegal characters in JSON response
提问by nnyby
I have a Sencha Touch app. One of the stores I have uses an ajax proxy and a json reader. Some of the strings in the JSON returned from my sinatra app occasionally contain this character: http://www.fileformat.info/info/unicode/char/2028/index.htm
我有一个煎茶触摸应用程序。我拥有的其中一家商店使用了 ajax 代理和 json 阅读器。从我的 sinatra 应用程序返回的 JSON 中的一些字符串偶尔包含这个字符:http: //www.fileformat.info/info/unicode/char/2028/index.htm
Although it's invisible, the character occurs twice in the second string here, between the period and the ending quote:
尽管它是不可见的,但该字符在此处的第二个字符串中出现了两次,在句点和结束引号之间:
"description": "Each of the levels requires logic, skill, and brute force to crush the enemy.??"
Try copy and pasting "Each of the levels requires logic, skill, and brute force to crush the enemy.??" into your javascript console! It won't be parsed as a string, and fails with SyntaxError: Unexpected token ILLEGAL
.
尝试复制和粘贴“每个关卡都需要逻辑、技巧和蛮力来粉碎敌人。??” 进入你的 javascript 控制台!它不会被解析为字符串,并且会失败SyntaxError: Unexpected token ILLEGAL
。
This causes the JSON response to fail. I've been stuck on this for a long time! Any suggestions?
这会导致 JSON 响应失败。我已经被困在这个问题上很长时间了!有什么建议?
采纳答案by Lasse
The only reliable way to fix this is server-side. Make sure your JSON generator emits those characters escaped, e.g. as \u2028
.
解决此问题的唯一可靠方法是服务器端。确保您的 JSON 生成器发出转义的字符,例如 as \u2028
。
In my experience, it's easiest to simply encode your JSON in plain ASCII which will always work. The downside is that it's less efficient as non-ASCII characters will take up more space, so depending on the frequency of those, you may not want that trade-off...
根据我的经验,最简单的方法是简单地用纯 ASCII 编码您的 JSON,这将始终有效。缺点是效率较低,因为非 ASCII 字符会占用更多空间,因此根据这些字符的频率,您可能不希望进行这种权衡...
The documentation for Perl's JSON::XS has a good explanation of the problem and advice for how to fix it in Perl: http://search.cpan.org/perldoc?JSON::XS#JSON_and_ECMAscript
Perl 的 JSON::XS 文档很好地解释了这个问题,并提供了如何在 Perl 中修复它的建议:http://search.cpan.org/perldoc?JSON::XS#JSON_and_ECMAscript
回答by Sam
Conceptually you are only allowed to send out strings from the server that are valid JavaScript literals by escaping appropriately.
从概念上讲,您只能通过适当的转义从服务器发出作为有效 JavaScript 文字的字符串。
If you want to fix this issue on the client you need an extra workaround step (only seems to work in Firefox):
如果你想在客户端解决这个问题,你需要一个额外的解决方法步骤(似乎只适用于 Firefox):
var a = escape("Each of the levels requires logic, skill, and brute force to crush the enemy.");
alert(unescape(a));
But the discussion is obsolete, because you must escape on the server.
但是讨论已经过时了,因为您必须在服务器上进行转义。
回答by Hello71
Avoid using eval
to parse JSON.
避免eval
用于解析 JSON。
Use JSON.parse
or https://github.com/douglascrockford/JSON-js.
使用JSON.parse
或https://github.com/douglascrockford/JSON-js。
JSON.parse('{}'); // where {} is your JSON String