0

我目前正在尝试从网站的 API JSON 输出中提取信息。这是我所拥有的,它几乎可以完美运行:

def get_player_stats
  uri = URI("http://elophant.com/api/v1/euw/getPlayerStats?accountId=#{CGI.escape(@summoner.acctId)}&season=CURRENT&key=KEYID")
  resp = Net::HTTP.get_response(uri)
  hash = JSON(resp.body)

  solo_ranked_elo = hash['playerStatSummaries']['playerStatSummarySet'][2]['maxRating']
  puts solo_ranked_elo

end

问题是 ['playerStatSummarySet'][1]值会根据玩家而改变。因此,对于一个玩家,他们maxRating将在 set 中[1],但另一名玩家maxRating将在 set 中[6]

我需要搜索RankedSolo5x5值存在的集合,然后我可以输出maxRating. 我该怎么办?

这是我用来比较的两个示例文件:

http://elophant.com/api/v1/euw/getPlayerStats?accountId=22031699&season=CURRENT&key=KEYID

http://elophant.com/api/v1/euw/getPlayerStats?accountId=23529170&season=CURRENT&key=KEYID

我希望这足够清楚!

4

1 回答 1

1

这是一个完整的例子

#!/usr/bin/env ruby

require 'net/http'
require 'uri'
require 'json'

uri = URI("http://elophant.com/api/v1/euw/getPlayerStats?accountId=#{ARGV[0]}&season=CURRENT&key=KEYID")
resp = Net::HTTP.get_response(uri)
stat_summary = JSON(resp.body)['playerStatSummaries']['playerStatSummarySet']

stat_summary.each_with_index do |obj, i| # it's this loop that answers your question
  next if obj['playerStatSummaryType'] != 'RankedSolo5x5'

  puts obj['maxRating']
  break
end

ARGV[0]是 的命令行参数值accountID。您将上述内容保存到某个max_rating文件中,chmod +x max_rating然后运行

./max_rating 22031699       # Outputs 1421
./max_rating 23529170       # Outputs 1237
于 2012-11-10T02:47:13.300 回答