Javascript JSON 解析错误:未终止的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27656389/
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 error: Unterminated string
提问by jdawg
I've met an usual problem when escaping a quote within the JSON parse function. If the escaped quote is present, in this case 'test"', it causes the following error 'SyntaxError: JSON Parse error: Unterminated string'.
在 JSON 解析函数中转义引号时,我遇到了一个常见问题。如果存在转义引号,在本例中为“test”,则会导致以下错误“语法错误:JSON 解析错误:未终止的字符串”。
var information = JSON.parse('[{"-1":"24","0":"","1":"","2":"","3":"0.0000","4":"","5":"0.00","6":"0.00","7":"1.00","8":"0","9":"false","10":"false","11":[""],"12":"","13":"","14":"test\""}]');
var information = JSON.parse('[{"-1":"24","0":"","1":"","2":"","3":"0.0000","4":"","5":"0.00","6":"0.00","7":"1.00","8":"0","9":"false","10":"false","11":[""],"12":"","13":"","14":"test\""}]');
JSON Lint validates the JSON as valid.
JSON Lint 验证 JSON 是否有效。
回答by adeneo
You'll have to double escape it, as in "test\\""
你必须双重逃脱它,如 "test\\""
var information = JSON.parse('[{"-1":"24","0":"","1":"","2":"","3":"0.0000","4":"","5":"0.00","6":"0.00","7":"1.00","8":"0","9":"false","10":"false","11":[""],"12":"","13":"","14":"test\""}]');
document.body.innerHTML = '<pre>' + JSON.stringify(information, null, 4) + '</pre>';
The first backslash escapes the second backslash in the javascript string literal. The second backslash escapes the quote in the JSON string literal.
第一个反斜杠转义了 javascript 字符串文字中的第二个反斜杠。第二个反斜杠转义 JSON 字符串文字中的引号。
So it's parsed twice, and needs escaping twice.
所以它被解析了两次,并且需要转义两次。
So even if it's valid JSON, you'll need one escape for javascript string literals that escapes the escape used in JSON.
因此,即使它是有效的 JSON,您也需要对 javascript 字符串文字进行一个转义,以转义 JSON 中使用的转义。
回答by qallidi mohammedamine
I fixed this problem in my spring boot app with @RestController by Using @IgnoreJson
我通过使用 @IgnoreJson 在我的 Spring Boot 应用程序中使用 @RestController 修复了这个问题
--------- class book ---------------
@OneToMany(mappedBy = "book")
private Set<Chapitre> chapitres = new TreeSet<>();
--------- class chapitre -----------
@ManyToOne(cascade = CascadeType.MERGE)
@JsonIgnore
private Book book;
回答by Tweety01
var information = JSON.parse('[{"-1":"24","0":"","1":"","2":"","3":"0.0000","4":"","5":"0.00","6":"0.00","7":"1.00","8":"0","9":"false","10":"false","11":[""],"12":"","13":"","14":"test\""}]');
Putting doubleslash worked for me...at least in console..give it a try..
把双斜杠对我有用......至少在控制台中......试试看......
回答by Naveen Kumar PG
Here is the issue : use \' instead of \" at last array of object
这是问题:在最后一个对象数组中使用 \' 而不是 \"
\"
\'

