admin管理员组文章数量:1353265
Bad control character in string literal in JSON at position 197 error is what i get when I try to parse this json string:
var obj = JSON.parse('[{"name":"Charles freed from Nepal jail","content":"A French serial killer known as The Serpent, convicted of several tourist murders in Asia in the 1970s, has been released from a Nepalese prison.\r\n\r\nCharles Sobhraj, 78, was freed after a court ruled in favour of his age and good behaviour.\r\n\r\nHe spent 19 years in jail in Nepal for killing two North Americans in 1975.","id":"1"}]');
And i really don't know what am I doing wrong.
I know for sure, that if I try to parse the json without a linebreak it works just fine, but when I try to have a content with a line break it just doen't work. I am not sure what the problem is here.
Bad control character in string literal in JSON at position 197 error is what i get when I try to parse this json string:
var obj = JSON.parse('[{"name":"Charles freed from Nepal jail","content":"A French serial killer known as The Serpent, convicted of several tourist murders in Asia in the 1970s, has been released from a Nepalese prison.\r\n\r\nCharles Sobhraj, 78, was freed after a court ruled in favour of his age and good behaviour.\r\n\r\nHe spent 19 years in jail in Nepal for killing two North Americans in 1975.","id":"1"}]');
And i really don't know what am I doing wrong.
I know for sure, that if I try to parse the json without a linebreak it works just fine, but when I try to have a content with a line break it just doen't work. I am not sure what the problem is here.
Share Improve this question asked Dec 23, 2022 at 11:14 NorbiNorbi 351 gold badge1 silver badge7 bronze badges 4- 1 Does this answer your question? Are multi-line strings allowed in JSON? – phuzi Commented Dec 23, 2022 at 11:25
-
You will need to encode/escape the carriage return
\r
character inside the string value as they are not allowed as you currently have them. – phuzi Commented Dec 23, 2022 at 11:26 - 1 So what's really happening here? Why are you using a JSON string in your Javascript source code? Is it being injected into the code by a server process? If so, the fix is to ensure that the injected JSON string is correctly escaped when it is injected, not to try and fix subsequently in the consuming JS. – spender Commented Dec 23, 2022 at 11:44
-
This code is redundant, it doesn't make a lot of sense to enter a JSON string with the sole purpose of producing a JavaScript object. Just type that same input as raw object and skip the
JSON.parse()
part. If it's just a simplification to illustrate the problem, it might be too simplified since\r\n
are escape sequences in many languages. – Álvaro González Commented Dec 24, 2022 at 10:57
2 Answers
Reset to default 4You just have to write double \\
var obj = JSON.parse(`[{"name":"Charles freed from Nepal jail","content":"A French serial killer known as The Serpent, convicted of several tourist murders in Asia in the 1970s, has been released from a Nepalese prison.\\r\\n\\r\\nCharles Sobhraj, 78, was freed after a court ruled in favour of his age and good behaviour.\\r\\n\\r\\nHe spent 19 years in jail in Nepal for killing two North Americans in 1975.","id":"1"}]`);
The issue is you have to escape your new line character with another slash,
eg \r\n
var obj = JSON.parse('[{"name":"Charles freed from Nepal jail","content":"A French serial killer known as The Serpent, convicted of several tourist murders in Asia in the 1970s, has been released from a Nepalese prison.\\r\\n\\r\\nCharles Sobhraj, 78, was freed after a court ruled in favour of his age and good behaviour.\\r\\n\\r\\nHe spent 19 years in jail in Nepal for killing two North Americans in 1975.","id":"1"}]')
本文标签: javascriptBad control character in string literal in JSON at position 197 errorStack Overflow
版权声明:本文标题:javascript - Bad control character in string literal in JSON at position 197 error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743915886a2561206.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论