如何解决 JSON.parse: bad control character in string literal,在这段代码中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17487058/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:55:10  来源:igfitidea点击:

How to solve JSON.parse: bad control character in string literal, in this code

javascriptjson

提问by user2553396

I'm a beginner at JSONdocument, this is my code so please help me to solve this error.

我是JSON文档的初学者,这是我的代码,所以请帮我解决这个错误。

{
   "_id" : "_design/utilisateur",
   "_rev" : "1-967a00dff5e02add41819138abb3284d",
   "views" : {
    "tous" : { 
        "map" : "function(doc){if (doc.role=='utilisateur') {emit (doc._id,        [['t0',doc.distancet0],['t1',doc.distancet1],['t2',doc.distancet2],['t3',doc.distancet3],    ['t4',doc.distancet4]])}}"
         }, 

    "3500" : {
        "map" : "function(doc) {if (doc.role=='utilisateur' &&     doc.distancet0<3500) {emit(doc._id,doc.distancet0)}}"
        },

        "distancetot" : {
                "map" : "function(doc) {var somme= Math.abs(doc.distancet0-    doc.distancet1); if(doc.role=='utilisateur'){ 
  emit(doc._id, somme);
}}"
                          }    
}           

}

回答by T.J. Crowder

The error message is telling you that you have a control character within a string literal, for instance, character code 8 or 10 or 13 or anything below 32 (a space).

错误消息告诉您字符串文字中有一个控制字符,例如,字符代码 8 或 10 或 13 或任何低于 32 的字符(空格)。

The JSON definitiontells us that you cannot have literal control characters in string literals, you must use an escape sequence such as \b, \r, \n, or \uXXXXwhere XXXXis a hex code for a Unicode "code point" (character).

该JSON定义告诉我们,你不能在字符串字面控制字符,则必须使用转义序列,如\b\r\n,或\uXXXX在那里XXXX是一个Unicode“码点”十六进制代码(字符)。

So for instance, pretend the following is in a file (or other data stream):

例如,假设以下内容在文件(或其他数据流)中:

{
    "property": "value with an invalid
control character in it"
}

That's invalid JSON, the string literal starting with "valuehas at least one control character in it (the line break, might be one or two control characters depending on the OS).

这是无效的 JSON,以 开头的字符串文字"value中至少有一个控制字符(换行符,可能是一两个控制字符,具体取决于操作系统)。

This is how we would fix it:

这是我们将如何修复它:

{
    "property": "value with an valid\nescape sequence in it"
}

Note the \nwhere the line break used to be.

注意\n以前换行的位置。

You can use http://jsonlint.comto validate JSON, it's quite good at pointing out where the error is.

您可以使用http://jsonlint.com来验证JSON,它非常擅长指出错误所在。



Re your edit: It is indeed a line break causing the problem:

重新编辑:这确实是导致问题的换行符:

"distancetot": {
    "map": "function(doc) {var somme= Math.abs(doc.distancet0-    doc.distancet1); if(doc.role=='utilisateur'){ 
Error is here -------------------------------------------------------------------------------------------------^

The line break after if(doc.role=='utilisateur'){is an invalid control character, just as in my example above.

后面的换行符if(doc.role=='utilisateur'){是一个无效的控制字符,就像我上面的例子一样。