简体   繁体   中英

grabbing data deep within a nested Json Object

I have data in the form of a nested JSON object. I want to grab some of the data to create a table, but it's deeply nested in arrays and objects. What's the best way to go about this for my table?

If I wanted to grab the team names, washington Nationals and Miami Marlins, how do I go about referencing those properties? I'm using Angular 1 with ng repeat to iterate.

Here's an example of the first part of a much longer json object:

{
  "sports": [
    {
      "name": "baseball",
      "id": 1,
      "uid": "s:1",
      "leagues": [
        {
          "name": "Major League Baseball",
          "abbreviation": "mlb",
          "id": 10,
          "uid": "s:1~l:10",
          "groupId": 9,
          "shortName": "MLB",
          "events": [
            {
              "id": 350506120,
              "uid": "s:1~l:10~e:350506120",
              "date": "2015-05-06T17:05:00Z",
              "season": {
                "year": 2015,
                "type": 2,
                "description": "regular",
                "startDate": "2015-04-05T07:00:00Z",
                "endDate": "2015-10-05T06:59:59Z"
              },
              "timeValid": true,
              "competitions": [
                {
                  "id": 350506120,
                  "uid": "s:1~l:10~c:350506120",
                  "date": "2015-05-06T17:05:00Z",
                  "timeValid": true,
                  "competitors": [
                    {
                      "type": "team",
                      "score": 7,
                      "homeAway": "home",
                      "isWinner": true,
                      "team": {
                        "id": 20,
                        "uid": "s:1~l:10~t:20",
                        "location": "Washington",
                        "name": "Nationals",
                        "nickname": "Washington",
                        "abbreviation": "WSH",
                        "color": "0a295d",
                        "links": {
                          "api": {
                            "teams": {
                              "href": "http://api-partners.espn.com/v1/sports/baseball/mlb/teams/20"
                            }
                          }
                        },
                        "record": {
                          "summary": "14-15",
                          "wins": 14,
                          "losses": 15,
                          "overtimeLosses": 1,
                          "ties": 0
                        }
                      }
                    },
                    {
                      "type": "team",
                      "score": 5,
                      "homeAway": "away",
                      "isWinner": false,
                      "team": {
                        "id": 28,
                        "uid": "s:1~l:10~t:28",
                        "location": "Miami",
                        "name": "Marlins",
                        "nickname": "Miami",
                        "abbreviation": "MIA",
                        "color": "0081c7",
                        "links": {
                          "api": {
                            "teams": {
                              "href": "http://api-partners.espn.com/v1/sports/baseball/mlb/teams/28"
                            }
                          }
                        },

// ....

thank you!

Using Array map() function :

var res = obj.sports[0].leagues[0].events[0].competitions[0].competitors.map(function(item) {
  return item.team.location+ " " +item.team.name;
});

console.log(res);

Using for in loop :

var resArray = [];
for (var i in obj.sports[0].leagues[0].events[0].competitions[0].competitors) {          
  resArray.push(obj.sports[0].leagues[0].events[0].competitions[0].competitors[i].team.location+ " " +obj.sports[0].leagues[0].events[0].competitions[0].competitors[i].team.name);
}

console.log(resArray);

Working demo :

 var obj = { "sports": [ { "name": "baseball", "id": 1, "uid": "s:1", "leagues": [ { "name": "Major League Baseball", "abbreviation": "mlb", "id": 10, "uid": "s:1~l:10", "groupId": 9, "shortName": "MLB", "events": [ { "id": 350506120, "uid": "s:1~l:10~e:350506120", "date": "2015-05-06T17:05:00Z", "season": { "year": 2015, "type": 2, "description": "regular", "startDate": "2015-04-05T07:00:00Z", "endDate": "2015-10-05T06:59:59Z" }, "timeValid": true, "competitions": [ { "id": 350506120, "uid": "s:1~l:10~c:350506120", "date": "2015-05-06T17:05:00Z", "timeValid": true, "competitors": [ { "type": "team", "score": 7, "homeAway": "home", "isWinner": true, "team": { "id": 20, "uid": "s:1~l:10~t:20", "location": "Washington", "name": "Nationals", "nickname": "Washington", "abbreviation": "WSH", "color": "0a295d", "links": { "api": { "teams": { "href": "http://api-partners.espn.com/v1/sports/baseball/mlb/teams/20" } } }, "record": { "summary": "14-15", "wins": 14, "losses": 15, "overtimeLosses": 1, "ties": 0 } } }, { "type": "team", "score": 5, "homeAway": "away", "isWinner": false, "team": { "id": 28, "uid": "s:1~l:10~t:28", "location": "Miami", "name": "Marlins", "nickname": "Miami", "abbreviation": "MIA", "color": "0081c7", "links": { "api": { "teams": { "href": "http://api-partners.espn.com/v1/sports/baseball/mlb/teams/28" } } } } } ] } ] } ] } ] } ] }; var resArray = []; for (var i in obj.sports[0].leagues[0].events[0].competitions[0].competitors) { resArray.push(obj.sports[0].leagues[0].events[0].competitions[0].competitors[i].team.location+ " " +obj.sports[0].leagues[0].events[0].competitions[0].competitors[i].team.name); } console.log(resArray); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM