Why is it that whenever I do :-

JSON.parse('"something"')

it just parses fine but when I do:-

var m = "something";

JSON.parse(m);

it gives me an error saying:-

Unexpected token s

 

【回答】

 

You're asking it to parse the JSON text something (not "something"). That's invalid JSON, strings must be in double quotes.

If you want an equivalent to your first example:

var s = '"something"';

var result = JSON.parse(s);

 

来自:https://stackoverflow.com/questions/18791718/json-parse-unexpected-token-s

posted on 2018-05-05 14:25  今夜太冷  阅读(454)  评论(0编辑  收藏  举报