javascript JSON.parse UTF-8 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27059765/
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
JSON.parse UTF-8 string
提问by Karim Benhmida
I'm trying to parse a UTF-8 encoded string, but JSON.parse is choking on this line:
我正在尝试解析一个 UTF-8 编码的字符串,但 JSON.parse 在这一行上令人窒息:
undefined:306 , "o": { "type": "uri", "value": "http://got.dbpedia.org/resource/\U00010331\ ^ SyntaxError: Unexpected token U
undefined:306, "o": { "type": "uri", "value": " http://got.dbpedia.org/resource/\U00010331\ ^ SyntaxError: Unexpected token U
this is the line I use:
这是我使用的线路:
var object = JSON.parse(data);
I've also tried with this line, but no success:
我也试过这条线,但没有成功:
var object = JSON.parse(data.toString('utf8'));
Any idea?
任何的想法?
采纳答案by Quentin
In a JSON string a \
character may not be followed by an uppercase U
character. Your JSON is invalid. You need a lowercase u
instead and should fix whatever is outputting the invalid JSON.
在 JSON 字符串中,\
字符后面不能跟大写U
字符。您的 JSON 无效。您需要一个小写字母,u
并且应该修复输出无效 JSON 的任何内容。