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

javascript - Why am I am getting Unexpected token- Stack Overflow

programmeradmin0浏览0评论
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

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

Share Improve this question asked Sep 7, 2013 at 20:25 user2621381user2621381 1031 silver badge9 bronze badges 6
  • 1 Just pasted the code into the console, gave me no error... Ok, can you try to locate the exact location of that token, by reducing the serialized dataset? – raina77ow Commented Sep 7, 2013 at 20:29
  • 1 try your code in the console, it works fine. That said, why are you using \" all over the place instead of just using ' as outer quotes so you don't have to escape all those double quotes? – Mike 'Pomax' Kamermans Commented Sep 7, 2013 at 20:38
  • @raina77ow Please see this, picpaste./pics/… – user2621381 Commented Sep 7, 2013 at 20:38
  • @Mike'Pomax'Kamermans it is server generated, I am struggling with this for 6 hours.please see this pic picpaste./pics/… – user2621381 Commented Sep 7, 2013 at 20:41
  • This was discussed on chat like 4 hours ago! – iConnor Commented Sep 7, 2013 at 20:51
 |  Show 1 more ment

2 Answers 2

Reset to default 4

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.

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.

You could also replace the invalid ecape sequences client-side, but I would not remend this:

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

Based on your image from the ments, 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:

>>> [{\"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 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.

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);
发布评论

评论列表(0)

  1. 暂无评论