JSON.parse意外令牌 [英] JSON.parse unexpected token s
本文介绍了JSON.parse意外令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么每当我这样做: -
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
推荐答案
你要求它解析JSON文本某事
(不是某事
)。这是无效的JSON,字符串必须是双引号。
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);
这篇关于JSON.parse意外令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文