访问数组中的JSON对象

时间:2017-10-23 02:08:43

标签: javascript json node.js

我已经阅读了关于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);
    }
}

2 个答案:

答案 0 :(得分:1)

您的帖子不包含问题。我将假设问题&#34;如何访问数组中的对象?&#34;和&#34;为什么我的代码不起作用?&#34;。

也许这个例子会为你澄清一下。

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

答案 1 :(得分:1)

您正在尝试访问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

检查属性赔率和acesport是否存在。这更具可扩展性。

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