嗨,我对存储一些 json 数据的本地文件有疑问
{
"tournament": [{
"TeamName": "AS Roma",
"TeamPlayer": "Rickard"
} {
"TeamName": "Inter",
"TeamPlayer": "Bobban"
}]
}
然后在buttonclick上我尝试用这个数据填充一个数组。但它只需要“AS Roma”和“Rickard”而不是“As Roma”“Rickard”和“Inter”“Bobban”。
// Get teams
var url = "http://localhost:57608/json/teams.txt";
$("#btnGetTeams").on('click', function() {
var allItems = [];
$.getJSON(url, function(data) {
$.each(data, function(i, team) {
allItems.push({
theTeam: team.tournament.TeamName,
thePlayer: team.tournament.TeamPlayer
});
});
$.each(allItems, function(i, val) {
$('#teams').append('<p>' + val.theTeam + val.thePlayer + '</p>');
});
});
});
因此,它不会输出 AS Roma Rickard Inter Bobban,而是只写 AS Roma Rickard。我究竟做错了什么?
感谢 Sushanth 更新完整的工作代码
// Get teams
var url = "http://localhost:57608/json/teams.txt";
$("#btnGetTeams").on('click', function () {
var allItems = [];
$.getJSON(url,
function (data) {
$.each(data.tournament, function (i) {
allItems.push({
theTeam: data["tournament"][i]["TeamName"],
thePlayer: data["tournament"][i]["TeamPlayer"],
});
});
$.each(allItems, function (i, val) {
$('#teams').append('<p>' + val.theTeam + val.thePlayer + '</p>');
});
});
});
与 Json
{
"tournament": [
{
"TeamName": "AS Roma",
"TeamPlayer": "Rickard"
},
{
"TeamName": "Inter",
"TeamPlayer": "Bobban"
}
]
}