Reputation: 398
I have a JSON object below and there are multiple nested divisions, the first without any identifiers. I am trying to get to the teams within the last divisions using a for loop, but I am not able to access them. Need some guidance.
{
"division": {
"division": [
{
"team": {
"id": "229525",
"name": "MyTeam",
"photo": "",
"visible": "True",
"RosterView": "True",
"PublicResults": "True",
"Statistics": "False",
"privilege": [
"False",
"True",
"True",
"True",
"True",
"True",
"True"
]
},
"name": "Boys 9-10",
"id": "12897",
"sort": "0",
"open": "0"
},
{
"team": [
{
"id": "229523",
"name": "Cougars",
"photo": "",
"visible": "True",
"RosterView": "True",
"PublicResults": "True",
"Statistics": "False",
"privilege": [
"False",
"True",
"True",
"True",
"True",
"True",
"True"
]
},
Upvotes: 2
Views: 22636
Reputation: 578
Update: Hmmm, after reading your JSON carefully, I notice that the 1st division has only 1 team since the team has only one object. But seems like the 2nd division has more than 1 team since there is an array of objects in team. I guess you have to handle the case where there only one team in a division.
Your for loop was almost right. Try this:
var divisions = JSONObject.division.division;
for (var i=0; i < divisions.length; i++) {
// handle division of one team here,
// don't need for loop if there only one team
// just do division[i].team.id
for (var j=0; j < divisions[i].length; j++) {
var teamId = divisions[i].team[j].id;
alert(teamId);
}
}
Your loop was
for (i=0; i <= JSONObject.division.division.teams[i].length; i++) { }
It has 2 problems, first you are skipping the last element by doing i <= ...length; i++
second, JSONObject.division.division.teams[i]
is not an array, but this one JSONObject.division.division[i].team
will give you an array of all the teams in a division. Then you can just get the team you want by its index in the array :)
Upvotes: 2
Reputation: 7917
First you should fix your parenthesis in json.
var a = {
"division": {
"division": [
{
"team": {
"id": "229525",
"name": "MyTeam",
"photo": "",
"visible": "True",
"RosterView": "True",
"PublicResults": "True",
"Statistics": "False",
"privilege": [
"False",
"True",
"True",
"True",
"True",
"True",
"True"
]
},
"name": "Boys 9-10",
"id": "12897",
"sort": "0",
"open": "0"
},
{
"team": [
{
"id": "229523",
"name": "Cougars",
"photo": "",
"visible": "True",
"RosterView": "True",
"PublicResults": "True",
"Statistics": "False",
"privilege": [
"False",
"True",
"True",
"True",
"True",
"True",
"True"
]
}
]
}
]
}
}
Then you can reach second 'team' object with:
a['division']['division'][1]['team'][0]['id'];
This will give you id
. You can select anything you want. It is not so complicated. Just read it like a puzzle.
Here is live example: JSFiddle
here are some for loops :
var json1 = a['division']['division'][1]['team'][0]; // this is for second team array,
json2 = a['division']['division'][0]['team']; // this is for first team object,
for (obj in json1){
return json[obj];
};
According to your json, first and second teams
are not in same data type. first team is array but second one is object. Thats why there are two different variables.
Upvotes: 1