好的,这是我的HTML代码:
<div id="contentholder">
<div class="personcard">
<img src="http://placehold.it/400x400" />
<h2>Matej Vydra <span>Striker</span></h2>
<p>This is a paragraph about Watford Striker Matej Vydra, it doesnt really make much sense, but its great for testing and lolz.</p>
</div>
</div>
这是JS和JSON文件:
$(document).ready(function () {
$.getJSON('json/currentsquad.json', function (currentsquad) {
var output = "<div class='personcard'>";
for (var i in currentsquad.players) {
output += "<img src='" + currentsquad.players[i].image + "'/> <h2>" + currentsquad.players[i].name + "<span>" + currentsquad.players[i].position + "</span> </h2>";
}
output += "</div>";
document.getElementById("contentholder").innerHTML = output;
});
});
JSON:
{
"players": [
{
"image": "http://placehold.it/400x400",
"name": "Matej Vydra",
"position": "Striker"
},
{
"image": "http://placehold.it/400x400",
"name": "Troy Deeney",
"position": "Striker"
},
]
}
问题是代码没有显示给用户,我之前尝试过搜索,因此尝试在localhost上运行它,但问题保持不变,并且JSON没有显示给用户。 提前谢谢。
答案 0 :(得分:2)
您的JSON无效。虽然JavaScript对象文字表示法允许使用尾随逗号,但JSON标准却不允许。因此,如果这是您正在发送的JSON的准确表示,则您需要删除players
数组中最后一个对象之后的逗号。
{
"players": [
{
"image": "http://placehold.it/400x400",
"name": "Matej Vydra",
"position": "Striker"
},
{
"image": "http://placehold.it/400x400",
"name": "Troy Deeney",
"position": "Striker"
}
]
}