-1

我有一个字符串,我试图将其转换为 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

4 回答 4

8

这是完全无效的 JSON。必须引用每个键。例如

[{"name":"After","value":1}, ....
  ^--  ^--       ^--   ^--
于 2013-02-27T21:01:02.057 回答
1

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

于 2013-02-27T21:06:30.503 回答
1

它不是有效的 json。你需要像这样在你的键周围加上引号

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

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

于 2013-02-27T21:01:47.093 回答
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}]
于 2013-02-27T21:02:00.070 回答