我正在使用AngularJS'http()
方法加载位于我计算机上的JSON文件。该文件的内容是:
{
"topics": [
{
"canonical": "Nebraska Football",
"alias": "NEB",
"score": 45,
},
{
"canonical": "Ohio State Football",
"alias": "OSU",
"score": 10,
}
]
}
当我加载文件(名为example.json)时,我在使用Error: JSON.parse: expected double-quoted property name
的Firefox和Safari时收到错误消息。在Chrome中(正如预期的那样,由于其默认安全性),它只是简单的不起作用。
JSON最初在Firefox中正常加载,但在浏览器更新后它停止工作。
我正在调用JSON:
$http.get("json/example.json").success(function(data) {
console.log(data);
});
我尝试使用Python的SimpleHTTPServer来运行网站,但它并没有什么区别。如果有人对这里出了什么问题有任何猜测,我真的很感激帮助。谢谢!
答案 0 :(得分:3)
Json无效 尝试
{
"topics": [
{
"canonical": "Nebraska Football",
"alias": "NEB",
"score": 45
},
{
"canonical": "Ohio State Football",
"alias": "OSU",
"score": 10
}
]
}
答案 1 :(得分:1)
如果您通过jsonlint.com运行JSON,则会将错误指向},{
。如果你从那里向上看,你会看到不应该在那里的尾随,
。你的JSON应该是:
{
"topics": [
{
"canonical": "Nebraska Football",
"alias": "NEB",
"score": 45
},
{
"canonical": "Ohio State Football",
"alias": "OSU",
"score": 10
}
]
}
答案 2 :(得分:0)
修改后的代码:从上一项删除','可以解决您的问题
{
"topics": [
{
"canonical": "Nebraska Football",
"alias": "NEB",
"score": 45
},
{
"canonical": "Ohio State Football",
"alias": "OSU",
"score": 10
}
]
}
答案 3 :(得分:0)
您的问题是您的JSON中有额外的,
秒。修改如下:
{
"topics": [
{
"canonical": "Nebraska Football",
"alias": "NEB",
"score": 45
},
{
"canonical": "Ohio State Football",
"alias": "OSU",
"score": 10
}
]
}
答案 4 :(得分:0)
JSON Lint是验证JSON的好地方。它说你应该删除每个数组元素中最后一个值后的逗号,
。因此"score":45,
应为"score":45