{name:“Name”,值:1}是无效的JSON?

时间:2013-02-27 20:59:40

标签: javascript json

我有一个字符串,我正在尝试将其转换为JSON对象并使用语法var json = JSON.parse(myjson);,但它说的是预期令牌}但我无法看到哪里?

有谁知道这会是什么?

[{name:"After", value:1},{name:"watching", value:1},{name:"you", value:1},{name:"at", value:3},{name:"Birmngm", value:1},{name:"my", value:1},{name:"new", value:2},{name:"athlete", value:1},{name:"to", value:1},{name:"follow.", value:1},{name:"Love", value:2},{name:"the", value:4},{name:"passion,", value:1},{name:"enthusiasm", value:1},{name:"and", value:3},{name:"big", value:1},{name:"smiles!!", value:1},{name:"Long", value:1},{name:"may", value:1}]

4 个答案:

答案 0 :(得分:8)

完全无效的JSON。每个键都必须引用。 e.g。

[{"name":"After","value":1}, ....
  ^--  ^--       ^--   ^--

答案 1 :(得分:1)

这是无效的json。你需要在你的钥匙周围加上引号

{
    "name": "After",
    "value": 1
},

JSONLint是调试JSON问题的绝佳工具,它可以告诉您特定字符串是否有效并指出问题所在。

答案 2 :(得分:1)

这是有效的JavaScript,不是有效的JSON。您可以打开chromes开发人员控制台,并将其字符串化以获取有效的JSON:

[{"name":"After","value":1},{"name":"watching","value":1},{"name":"you","value":1},{"name":"at","value":3},{"name":"Birmngm","value":1},{"name":"my","value":1},{"name":"new","value":2},{"name":"athlete","value":1},{"name":"to","value":1},{"name":"follow.","value":1},{"name":"Love","value":2},{"name":"the","value":4},{"name":"passion,","value":1},{"name":"enthusiasm","value":1},{"name":"and","value":3},{"name":"big","value":1},{"name":"smiles!!","value":1},{"name":"Long","value":1},{"name":"may","value":1}]

答案 3 :(得分:1)

如果您尝试构建JS对象,则提供的字符串是正确的。但是,为了创建有效的JSON字符串,您需要用引号括起键。因此,您应该将namevalue封装在双引号或单引号中。您可以使用jsonlint.com进行验证。