javascript - 抓取嵌套 Json 对象深处的数据

标签 javascript angularjs arrays json

我有嵌套 JSON 对象形式的数据。我想抓取一些数据来创建一个表,但它深深嵌套在数组和对象中。对于我的 table 来说,最好的解决办法是什么?

如果我想获取球队名称,华盛顿国民队和迈阿密马林鱼队,我该如何引用这些属性?我使用 Angular 1 和 ng Repeat 来迭代。

这是一个更长的 json 对象的第一部分的示例:

{
  "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"
                            }
                          }
                        },

// ....

谢谢!

最佳答案

使用Array map()函数:

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);

使用for in循环:

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);

工作演示:

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);

关于javascript - 抓取嵌套 Json 对象深处的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40714151/

相关文章:

javascript - 使用 Javascript 链接在 Google Universal Analytics 中进行跨域跟踪

arrays - 将字符串数组的单个元素初始化为Go lang中的另一个字符串变量

arrays - C:定义具有 64 位对齐起始地址的数组的可移植方法?

c - 我自己的数组中的随机整数和字符

JavaScript 数组

javascript - 我的自定义函数中的 For 循环不起作用

javascript - 替代 jquery .inarray?

javascript - Angular 指令 - NgModelCtrl 解析器问题

javascript - 如何将值添加到具有相同定义的另一个对象的对象属性在 JavaScript 中

javascript - Cordova + Angularjs - 为什么从回调内部调用的 $resource(或 $http)给出 404?