Accessing a specific item in a JSON return in Rails

Simonp27

New Member
#1
I have this JSON from the NHL API, and I tried to access the value gamesPlayed:
Mã:
"teams"=>[
  {
    "id"=>5,
    "name"=>"Pittsburgh Penguins",
    "link"=>"/api/v1/teams/5",
    "venue"=>{
      "id"=>5034,
      "name"=>"PPG Paints Arena",
      "link"=>"/api/v1/venues/5034",
      "city"=>"Pittsburgh",
      "timeZone"=>{
        "id"=>"America/New_York",
        "offset"=>-5,
        "tz"=>"EST"
      }
    },
    "abbreviation"=>"PIT",
    "teamName"=>"Penguins",
    "locationName"=>"Pittsburgh",
    "division"=>{
      "id"=>18,
      "name"=>"Metropolitan",
      "nameShort"=>"Metro",
      "link"=>"/api/v1/divisions/18",
      "abbreviation"=>"M"
    },
    "conference"=>{
      "id"=>6,
      "name"=>"Eastern",
      "link"=>"/api/v1/conferences/6"
    },
    "franchise"=>{
      "franchiseId"=>17,
      "teamName"=>"Penguins",
      "link"=>"/api/v1/franchises/17"
    },
    "teamStats"=>[
      {
        "type"=>{
          "displayName"=>"statsSingleSeason"
        },
        "splits"=>[
          {
            "stat"=>{
              "gamesPlayed"=>16,
              "wins"=>7,
              "losses"=>6,
              "ot"=>3,
              "pts"=>17,
              "ptPctg"=>"53.1",
              "goalsPerGame"=>3.313,
              "goalsAgainstPerGame"=>3.063,
              "evGGARatio"=>1.0833,
              "powerPlayPercentage"=>"23.4",
              "powerPlayGoals"=>11.0,
              "powerPlayGoalsAgainst"=>8.0,
              "powerPlayOpportunities"=>47.0,
              "penaltyKillPercentage"=>"84.0",
              "shotsPerGame"=>32.625,
              "shotsAllowed"=>33.6875,
              "winScoreFirst"=>0.6,
              "winOppScoreFirst"=>0.167,
              "winLeadFirstPer"=>0.5,
              "winLeadSecondPer"=>1.0,
              "winOutshootOpp"=>0.333,
              "winOutshotByOpp"=>0.444,
              "faceOffsTaken"=>1035.0,
              "faceOffsWon"=>534.0,
              "faceOffsLost"=>501.0,
              "faceOffWinPercentage"=>"51.6",
              "shootingPctg"=>10.2,
              "savePctg"=>0.909
            },
            "team"=>{
              "id"=>5,
              "name"=>"Pittsburgh Penguins",
              "link"=>"/api/v1/teams/5"
            }
          },
          {
            "stat"=>{
              "wins"=>"24th",
              "losses"=>"15th",
              "ot"=>"9th",
              "pts"=>"24th",
              "ptPctg"=>"19th",
              "goalsPerGame"=>"8th",
              "goalsAgainstPerGame"=>"19th",
              "evGGARatio"=>"11th",
              "powerPlayPercentage"=>"10th",
              "powerPlayGoals"=>"22nd",
              "powerPlayGoalsAgainst"=>"4th",
              "powerPlayOpportunities"=>"31st",
              "penaltyKillOpportunities"=>"1st",
              "penaltyKillPercentage"=>"6th",
              "shotsPerGame"=>"12th",
              "shotsAllowed"=>"27th",
              "winScoreFirst"=>"15th",
              "winOppScoreFirst"=>"27th",
              "winLeadFirstPer"=>"27th",
              "winLeadSecondPer"=>"7th",
              "winOutshootOpp"=>"25th",
              "winOutshotByOpp"=>"25th",
              "faceOffsTaken"=>"25th",
              "faceOffsWon"=>"19th",
              "faceOffsLost"=>"6th",
              "faceOffWinPercentage"=>"8th",
              "savePctRank"=>"13th",
              "shootingPctRank"=>"12th"
            },
            "team"=>{
              "id"=>5,
              "name"=>"Pittsburgh Penguins",
              "link"=>"/api/v1/teams/5"
            }
          }
        ]
      }
    ],
    "shortName"=>"Pittsburgh",
    "officialSiteUrl"=>"http://pittsburghpenguins.com/",
    "franchiseId"=>17,
    "active"=>true
  }
}
I am working in ruby on rails and would like to access the gamesPlayed value.

So far I have:
Mã:
url = 'https://statsapi.web.nhl.com/api/v1/teams/5?expand=team.stats'
    uri = URI(url)
    response = Net::HTTP.get(uri)
    response = JSON.parse(response)

    @awayteamgamesplayed = response["teams"][0]["teamStats"]["stat"]["gamesPlayed"]
I can get to the team name using: response["teams"][away_team]["name"] but cant't work out gamesPlayed.

But it doesn't seem to work for gamesPlayed.
 

Admin

Administrator
Thành viên BQT
#2
Value of teamStats is an Array. You need to access it via an index. Same for splits
Mã:
response["teams"][0]["teamStats"][0]["splits"][0]["stat"]["gamesPlayed"]
# => 16
 
Top