如何解决JSON.parse:字符串文字中的错误控制字符,在此代码中 [英] How to solve JSON.parse: bad control character in string literal, in this code

查看:703
本文介绍了如何解决JSON.parse:字符串文字中的错误控制字符,在此代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

{
   "_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);
}}"
                          }    
}           

}


推荐答案

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

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

JSON定义告诉我们你不能在字符串文字中使用文字控制字符,你必须使用转义序列,例如 \ b \ r \ n ,或 \ uXXXX 其中 XXXX 是Unicode代码点(字符)的十六进制代码。

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

因此,例如,假装以下文件(或其他文件)数据流):

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

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

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

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

这就是我们修复它的方法:

This is how we would fix it:

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

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

Note the \n where the line break used to be.

您可以使用 http://jsonlint.com 来验证JSON,它非常适合指出错误的位置。

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

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

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

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

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

这篇关于如何解决JSON.parse:字符串文字中的错误控制字符,在此代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆