这是我的json文件,我正在尝试访问组内的属性,例如homeTeam和markerImage。
以及如何确定比赛中的组数。请帮忙!
{
"competition":{
"group1":[
{
"homeTeam":"Lawrence Library",
"markerImage":"images/red.png",
},
{
"homeTeam":"Hamilton Library",
"markerImage":"images/white.png",
},
{
"homeTeam":"Applebees",
"markerImage":"images/newcastle.png",
}
],
"group2":[
{
"homeTeam":"Lawrence Library",
"markerImage":"images/red.png",
},
{
"homeTeam":"Hamilton Library",
"markerImage":"images/white.png",
},
{
"homeTeam":"Applebees",
"markerImage":"images/newcastle.png",
}
]
}
}
最佳答案
要确定比赛对象中的组数,可以使用
Object.keys(obj.competition).length
console.log(Object.keys(obj.competition).length);
为了使团队陷入困境,请尝试以下操作
for(var x in Object(obj.competition.group1)){
var newObj = Object(obj.competition.group1[x]);
console.log(newObj.homeTeam + ' and '+ newObj.markerImage);
}