簡體   English   中英

訪問數組中的JSON對象

[英]Accessing a JSON object in an array

我已經完成了有關JSON的Lynda課程,並且確實四處搜尋以解決此錯誤; 但是,我想我的問題太具體了,我需要一個例子。

JSON格式

{
  "date": "2017-10-15",
  "league": "NHL",
  "odds": "spreads",
  "status": "200 OK",
  "updated": "2017-10-23 00:07 UTC",
  "games": [
    {
      "away": {
        "rot": "051",
        "odds": {
          "acesport": "-1.5 +178",
          "betphoenix": "-1.5 +177",
          "betus": "-1.5 +170",
          "bookmaker": "-1.5 +180",
          "bovada": "-1.5 +170",
          "dsi": "-1.5 +170",
          "easystreet": "-1.5 +180",
          "jazz": "-1.5 +175",
          "mirage": "-1.5 +180",
          "open": "-1.5 -110",
          "pinnacle": "-1.5 +192",
          "sbg": "-1.5 +175",
          "sia": "-1.5 +150",
          "sportsbet": "-1.5 +240",
          "station": "-1.5 +175",
          "westgate": "-1.5 +175"
        },
        "team": "Boston Bruins",
        "score": 1
      },
      "home": {
        "rot": "052",
        "odds": {
          "acesport": "+1.5 -208",
          "betphoenix": "+1.5 -217",
          "betus": "+1.5 -200",
          "bookmaker": "+1.5 -210",
          "bovada": "+1.5 -200",
          "dsi": "+1.5 -200",
          "easystreet": "+1.5 -210",
          "jazz": "+1.5 -205",
          "mirage": "+1.5 -220",
          "open": "+1.5 -110",
          "pinnacle": "+1.5 -214",
          "sbg": "+1.5 -210",
          "sia": "+1.5 -175",
          "sportsbet": "+1.5 -280",
          "station": "+1.5 -210",
          "westgate": "+1.5 -200"
        },
        "team": "Vegas Golden Knights",
        "score": 3
      },
      "time": "7:05 PM EST",
      "status": "Final"
    }
  ]
}

從搜索和視頻中收集的信息來看,我認為下面的代碼已經接近。 問題一定是這個inst只是數組中的值,而是另一個對象?

for (i = 0; i <= body.games.length; i++){
    for (var key in body.games[i]) {
        console.log("Game " + (i +1));
        console.log(body.games[i][key].away.odds.acesport);
    }
}

您的帖子沒有問題。 我將假設以下問題:“如何訪問數組中的對象?” 和“為什么我的代碼不起作用?”。

也許這個例子會為您澄清。

for (var i = 0; i <= body.games.length; i++) {
    var game = body.games[i];
    console.log("Game number " + (i + 1));
    console.log(game.away.odds.acesport);
    for (var key in game) {
        // This will print the strings "away", "home", "time" and "status",
        // but not the values game[key] which are "object", "object",
        // "7:05 PM EST" and "Final".
        console.log("Game property: " + key);
    }
}

您正在嘗試訪問body.games[i].away.away.odds.acesport ,但此方法body.games[i].away.away.odds.acesport 您應該嘗試一些方法:

方法1

只需獲取您知道具有所需屬性的2個鍵的值即可。

for (i = 0; i <= body.games.length - 1; i++){
  console.log("Game " + (i +1));
  console.log(body.games[i].away.odds.acesport);
  console.log(body.games[i].home.odds.acesport);
}

方法#2

檢查屬性賠率和aceport是否存在。 這更具可擴展性。

for (i = 0; i <= body.games.length - 1; i++) {
  console.log("Game " + (i +1));
  for (var key in body.games[i]) {  
    var acesport = body.games[i][key].odds && body.games[i][key].odds.acesport;
    if (acesport) console.log(acesport);
  }
}

方法3

現在,如果您想嘗試一點清潔劑:

body.games.forEach(function(game, index) {
  console.log("Game " + index + 1);
  Object.keys(game).forEach(function (prop) {
    var acesport = game[prop].odds && game[prop].odds.acesport;
    if (acesport) console.log(acesport);
  });
});

還是更干凈(如果可以使用字符串模板和箭頭函數):

body.games.forEach((game, index) => {
  console.log(`Game ${index + 1}`);
  Object.keys(game).forEach((prop) => {
    var acesport = game[prop].odds && game[prop].odds.acesport;
    if (acesport) console.log(acesport);
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM