【问题标题】:Accessing a specific item in a JSON return in Rails在 Rails 中访问 JSON 返回中的特定项目
【发布时间】:2018-11-15 07:11:54
【问题描述】:

我有来自 NHL API 的这个 JSON,我尝试访问值 gamesPlayed:

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

我正在使用 ruby​​ on rails 并希望访问 gamesPlayed 值。

到目前为止我有:

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"]

我可以使用:response["teams"][away_team]["name"] 获得球队名称,但无法计算出比赛的比赛。

但它似乎不适用于 gamesPlayed。

【问题讨论】:

    标签: ruby-on-rails json ruby


    【解决方案1】:

    teamStats 的值是一个数组。您需要通过索引访问它。 splits 也一样

    response["teams"][0]["teamStats"][0]["splits"][0]["stat"]["gamesPlayed"]
    # => 16 
    

    【讨论】:

      【解决方案2】:

      teamStats 是一个数组试试这个

      response["teams"][0]["teamStats"][0]["stat"]["gamesPlayed"]
      

      【讨论】:

      • 谢谢,但这不起作用,非常感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多