2016-11-21 79 views
3

我有嵌套JSON對象形式的數據。我想抓取一些數據來創建表,但它深深地嵌套在數組和對象中。什麼是最好的方式去這個爲我的桌子?在嵌套的Json對象內抓取數據

如果我想抓住隊名,華盛頓國民和邁阿密馬林魚隊,我該如何去引用這些屬性?我使用Angular 1與ng重複迭代。

這裏有一個更長的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" 
          } 
          } 
         }, 

// .... 

謝謝!

+0

請檢查並重新發布您的JSON對象 –

+0

你在SO後採取的樣子:http://stackoverflow.com/questions/31307084/angular-js-parsing-json-object它會給你有些開始。 –

回答

0

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

+0

Thanks @RohitJindal! –

+0

@JonathanZungre請標記爲正確,以便對其他人也有幫助。 –