简体   繁体   English

为什么我会收到意外令牌\\

[英]Why am I am getting Unexpected token \

k ="[{\"id\": 1, \"latitude\": \"52.511467\", \"longitude\": \"13.447179\", \"bearing\": \"0.000000\", \"speed\": \"0.000000\", \"device_status\": 0, \"timestamp\": \"2013-08-18 00:00:00\"}, {\"id\": 3, \"latitude\": \"53.511467\", \"longitude\": \"14.447179\", \"bearing\": \"1.000000\", \"speed\": \"1.000000\", \"device_status\": 2, \"timestamp\": \"2013-08-18 00:00:00\"}, {\"id\": 4, \"latitude\": \"54.511467\", \"longitude\": \"15.447179\", \"bearing\": \"1.000000\", \"speed\": \"1.000000\", \"device_status\": 2, \"timestamp\": \"2013-08-18 00:00:00\"}, {\"id\": 5, \"latitude\": \"33.511467\", \"longitude\": \"72.447179\", \"bearing\": \"1.000000\", \"speed\": \"1.000000\", \"device_status\": 1, \"timestamp\": \"2013-08-18 00:00:00\"}]"

R = JSON.parse(k)

SyntaxError: Unexpected token \\ 语法错误:意外的令牌\\

whereas

m =JSON.parse("[{\"id\": 2, \"da\": \"1\"}]")

is working without error 正在正常工作

It's because when it get's returned from the server, the result is getting store as a string, so the string itself contains the \\ escape character before every " . It isint valid to escape the " character in a JSON string. 这是因为从服务器返回时,结果将作为字符串存储,因此字符串本身在每个"之前都包含\\转义字符。对JSON字符串中的"字符进行转义是无效的。

It's like doing: 就像在做:

var json = '{\\"test\\":\\"test\\"}';
json //"{\"test\":\"test\"}"
JSON.parse(json); //SyntaxError: Unexpected token \

However, when you take that string and put it directly in the console, the \\ characters will play their escape role and will not be part of the actual string at the end. 但是,当您使用该字符串并将其直接放在控制台中时, \\字符将扮演其转义角色,并且不会在末尾成为实际字符串的一部分。 Basically, the JSON string produced server-side should not escape the " characters. 基本上,服务器端生成的JSON字符串不应转义"字符。

You could also replace the invalid ecape sequences client-side, but I would not recommend this: 您也可以在客户端替换无效的ecape序列,但我不建议这样做:

JSON.parse(json.replace(/\\"/g, '"'));

Based on your image from the comments, you didn't tell the whole story in your post. 根据评论中的图像,您没有在帖子中讲述整个故事。 If you do a console.log(k), you will notice it looks nothing like what you see in your console as output by currentTrackData: 如果您执行console.log(k),您会发现它看起来与currentTrackData输出在控制台中所看到的完全不同:

>>> [{\"id\": 1, \"latitude\": \"52.511467\", \"longitude\": \"13.447179\", \"bearing\": \"0.000000\", \"speed\": \"0.000000\", \"device_status\": 0, \"timestamp\": \"2013-08-18 00:00:00\"}, {\"id\": 3, \"latitude\": \"53.511467\", \"longitude\": \"14.447179\", \"bearing\": \"1.000000\", \"speed\": \"1.000000\", \"device_status\": 2, \"timestamp\": \"2013-08-18 00:00:00\"}, {\"id\": 4, \"latitude\": \"54.511467\", \"longitude\": \"15.447179\", \"bearing\": \"1.000000\", \"speed\": \"1.000000\", \"device_status\": 2, \"timestamp\": \"2013-08-18 00:00:00\"}, {\"id\": 5, \"latitude\": \"33.511467\", \"longitude\": \"72.447179\", \"bearing\": \"1.000000\", \"speed\": \"1.000000\", \"device_status\": 1, \"timestamp\": \"2013-08-18 00:00:00\"}]

tells us that the string contains \\" . If you assign that thing to a var, 告诉我们该字符串包含\\" 。如果您将该内容分配给var,

var k = "[{...\"..}]"
console.log(k);
>>> [{..."...}]

then you're explicitly solving the problem because those \\" are turned into " in your string. 那么您正在明确解决问题,因为您的字符串中的那些\\"变成了" If you pass the output straight into JSON.parse, then there are a million slashes in the input, making it illegal JSON. 如果将输出直接传递到JSON.parse,则输入中会有一百万个斜杠,使其成为非法JSON。

If this is server-generated, fix the generator. 如果这是服务器生成的,请修复生成器。 It shouldn't be escaping that string for you. 它不应该为您转义该字符串。 If you don't have that luxury, then do a string replacement. 如果您没有那么奢侈,请进行字符串替换。

var u = currentTrackData...;
u = u.replace(/\\"/g, '"');
JSON.parse(u);

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

相关问题 为什么我在JavaScript中将意外令牌变成非法? - Why am I getting Unexpected token illegal in javascript? 为什么我的React组件中出现意外令牌? - Why am I getting an unexpected token in my React component? 为什么我要科尔多瓦建立android问题“意外令牌”? - Why am I getting cordova build android issue 'unexpected token'? 为什么我得到了意外的令牌[在下面的...循环? - Why am I getting Unexpected token [ in the following for…of loop? 为什么我会收到 Uncaught SyntaxError:意外令牌:标识符 - Why am i getting Uncaught SyntaxError: unexpected token: identifier 为什么我得到:未捕获的SyntaxError:此处出现意外的令牌ILLEGAL - Why am I getting : Uncaught SyntaxError: Unexpected token ILLEGAL here 为什么我在这个JSX片段中得到“意外的令牌”? - Why am I getting “unexpected token” in this snippet of JSX? 为什么我在 JSON 中的位置 0 处收到 Unexpected token &lt; 的错误 - Why am I getting an error of Unexpected token < in JSON at position 0 为什么在 if 语句中使用逻辑 AND 时会得到意外标记? - Why am I getting an unexpected token when using logical AND in an if statement? 为什么会出现此意外的字符串错误? - Why am I getting this unexpected string error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM