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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:53:41  来源:igfitidea点击:

JSON.parse UTF-8 string

javascriptjsonnode.jsutf-8

提问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 Ucharacter. Your JSON is invalid. You need a lowercase uinstead and should fix whatever is outputting the invalid JSON.

在 JSON 字符串中,\字符后面不能跟大写U字符。您的 JSON 无效。您需要一个小写字母,u并且应该修复输出无效 JSON 的任何内容。

JSON Strings

JSON 字符串