最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Bad control character in string literal in JSON at position 197 error - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

You 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"}]')
发布评论

评论列表(0)

  1. 暂无评论