JSON Parse error: Unterminated string

一个人想着一个人 提交于 2019-12-10 09:25:57

问题


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'.

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.


回答1:


You'll have to double escape it, as in "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.

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.




回答2:


    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..




回答3:


I fixed this problem in my spring boot app with @RestController by Using @IgnoreJson

--------- class book ---------------
@OneToMany(mappedBy = "book")
private Set<Chapitre> chapitres = new TreeSet<>();
--------- class chapitre -----------
@ManyToOne(cascade = CascadeType.MERGE)
@JsonIgnore
private Book book;



回答4:


Here is the issue : use \' instead of \" at last array of object

\"
\'


来源:https://stackoverflow.com/questions/27656389/json-parse-error-unterminated-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!