简体   繁体   English

如何解决 JSON 解析错误“JSON.parse: 字符串文字中的错误控制字符”?

[英]How can I resolve JSON parsing error 'JSON.parse: bad control character in string literal'?

In NodeJS Backend, I send my data to client as:-在 NodeJS 后端,我将数据发送到客户端:-

res.end(filex.replace("<userdata>", JSON.stringify({name:user.name, uid:user._id, profile:user.profile}) ))

//No error here and Object is stringified perfectly //user is object returned in mongoDB's result //这里没有错误,对象被完美地字符串化 //user是mongoDB结果中返回的对象MongoDB 文档

The JSON string looks like this: JSON 字符串如下所示:

{"name":"Rishavolva","uid":"5f3ce234fd83024334050872","profile":{"pic":{"small_link":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXBsaWNhcyI6MiwidXJpcyI6W3siZGJfbmFtZSI6ImlmcmRiMDAxIiwidGFibGUiOiJGSUxFIiwiaWQiOjQ4fSx7ImRiX25hbWUiOiJpZnJkYjAwMiIsInRhYmxlIjoiRklMRSIsImlkIjo0OH1dLCJ1aWRfd2hpdGVsaXN0IjoiKiIsImlhdCI6MTU5ODE2MzMzNX0.9NkGnEumn4JW8IN0KFgxgN_6_4wN8qOgezNTyzz9osY","big_link":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXBsaWNhcyI6MiwidXJpcyI6W3siZGJfbmFtZSI6ImlmcmRiMDAxIiwidGFibGUiOiJGSUxFIiwiaWQiOjQ3fSx7ImRiX25hbWUiOiJpZnJkYjAwMiIsInRhYmxlIjoiRklMRSIsImlkIjo0N31dLCJ1aWRfd2hpdGVsaXN0IjoiKiIsImlhdCI6MTU5ODE2MzMzNX0.yxQ1GrhLsWPn8Qwu42EfTDXqaYwFtrM6f_7cAH2eLRY"},"aboutme":"I am Rishav Bhowmik\r\nand this is navratna pulaow"}}

and that UID is just a mongodb's primary key as string, and other two base 64 strings are just JWT tokens.并且那个 UID 只是一个 mongodb 的主键作为字符串,另外两个 base 64 字符串只是 JWT 令牌。

Now, when this JSON string reaches the Browser, I parse it with simple:现在,当这个 JSON 字符串到达​​浏览器时,我用简单的方法解析它:

JSON.parse(`<userdata>`)
//remember I used filex.replace("<userdata>", JSON.stringify...) in the server

For reference, my MongoDB Document here is:作为参考,我这里的 MongoDB 文档是:

Now when JSON.parse is executed on the JSON string it will look like this on final JS code.现在当在 JSON 字符串上执行 JSON.parse 时,它​​在最终的 JS 代码中看起来像这样。

JSON.parse(`{"name":"Rishavolva","uid":"5f3ce234fd83024334050872","profile":{"pic":{"small_link":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXBsaWNhcyI6MiwidXJpcyI6W3siZGJfbmFtZSI6ImlmcmRiMDAxIiwidGFibGUiOiJGSUxFIiwiaWQiOjQ4fSx7ImRiX25hbWUiOiJpZnJkYjAwMiIsInRhYmxlIjoiRklMRSIsImlkIjo0OH1dLCJ1aWRfd2hpdGVsaXN0IjoiKiIsImlhdCI6MTU5ODE2MzMzNX0.9NkGnEumn4JW8IN0KFgxgN_6_4wN8qOgezNTyzz9osY","big_link":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXBsaWNhcyI6MiwidXJpcyI6W3siZGJfbmFtZSI6ImlmcmRiMDAxIiwidGFibGUiOiJGSUxFIiwiaWQiOjQ3fSx7ImRiX25hbWUiOiJpZnJkYjAwMiIsInRhYmxlIjoiRklMRSIsImlkIjo0N31dLCJ1aWRfd2hpdGVsaXN0IjoiKiIsImlhdCI6MTU5ODE2MzMzNX0.yxQ1GrhLsWPn8Qwu42EfTDXqaYwFtrM6f_7cAH2eLRY"},"aboutme":"I am Rishav Bhowmik\r\nand this is navratna pulaow"}}`)

I get this error:我收到此错误:

Uncaught SyntaxError: JSON.parse: bad control character in string literal at line 1 column 702 of the JSON data

the string at position 702 of the JSON string is \\n JSON 字符串位置 702 处的字符串是\\n

First of all, how can \\n be a control character?首先, \\n怎么可能是控制字符?

What should I do to resolve this?我应该怎么做才能解决这个问题? Has this problem arrised due to MONGODB result?这个问题是不是因为 MONGODB 结果出现的?

\\n is a control character signifying a new line. \\n是表示换行的控制字符。 In JSON, those control characters (more specifically the \\ ) must be escaped inside strings.在 JSON 中,这些控制字符(更具体地说是\\ )必须在字符串中进行转义。

This will raise the error:这将引发错误:

JSON.parse(`{"hello":"world\n"}`)

This wont:这不会:

   JSON.parse(`{"hello":"world\\n"}`)

So one way would be to use something like replace to ensure your aboutme is properly escaped before JSON serialization.因此,一种方法是使用诸如replace东西来确保您的aboutme在 JSON 序列化之前正确转义。 See: How to escape a JSON string containing newline characters using JavaScript?请参阅: 如何使用 JavaScript 转义包含换行符的 JSON 字符串?

Ok have done some experimentation and have a solution.好的已经做了一些实验并有一个解决方案。 The Trick is to do JSON.stringify() twice,诀窍是执行JSON.stringify()两次,

Like,喜欢,

html_text.replace('/*<whatever>*/', JSON.stringify( JSON.stringify(the_object) ) )

If suppose html_text has a line which is如果假设 html_text 有一行是

<script>
const object_inbrowser = JSON.parse(/*<whatever>*/)
// no need to add qotes, `JSON.stringify` in the server will do that for you
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM