0

我是一名 Java 人,刚接触 Ruby。我一直在玩它只是为了看看它可以做什么,我遇到了一个我无法解决的问题。

我决定再次尝试 Sinatra,只是想看看它能做什么,并决定使用 ESPN API 看看我是否可以通过 API 拉出一个团队的场地。

我可以拨打电话并取回数据,但我无法解析它:

{"sports"=>[{"name"=>"baseball", "id"=>1, "uid"=>"s:1", "leagues"=>[{"name"=>"Major League Baseball", "abbreviation"=>"mlb", "id"=>10, "uid"=>"s:1~l:10", "groupId"=>9, "shortName"=>"MLB", "teams"=>[{"id"=>17, "uid"=>"s:1~l:10~t:17", "location"=>"Cincinnati", "name"=>"Reds", "abbreviation"=>"CIN", "color"=>"D60042", "venues"=>[{"id"=>83, "name"=>"Great American Ball Park", "city"=>"Cincinnati", "state"=>"Ohio", "country"=>"", "capacity"=>0}], "links"=>{"api"=>{"teams"=>{"href"=>"http://api.espn.com/v1/sports/baseball/mlb/teams/17"}, "news"=>{"href"=>"http://api.espn.com/v1/sports/baseball/mlb/teams/17/news"}, "notes"=>{"href"=>"http://api.espn.com/v1/sports/baseball/mlb/teams/17/news/notes"}}, "web"=>{"teams"=>{"href"=>"http://espn.go.com/mlb/team/_/name/cin/cincinnati-reds?ex_cid=espnapi_public"}}, "mobile"=>{"teams"=>{"href"=>"http://m.espn.go.com/mlb/clubhouse?teamId=17&ex_cid=espnapi_public"}}}}]}]}], "resultsOffset"=>0, "resultsLimit"=>50, "resultsCount"=>1, "timestamp"=>"2013-08-04T14:47:13Z", "status"=>"success"}

我想提取对象的场所部分,特别是名称值。每次我尝试解析它时,我最终都会收到“无法从零变为字符串”的错误,然后我也得到了一个整数到字符串的错误。

这是我到目前为止所拥有的:

get '/venue/:team' do

    id = ids[params[:team]]
    url = 'http://api.espn.com/v1/sports/baseball/mlb/teams/' + id + '?enable=venues&apikey=' + $key
    resp = Net::HTTP.get_response(URI.parse(url))
    data = resp.body
    parsed = JSON.parse(resp.body)
    #venueData = parsed["sports"]

    "Looking for the venue of the #{params[:team]}, which has id " + id + ", and here's the data returned: " + venueData.to_s

end

当我做 parsed["sports"} 我得到:

 [{"name"=>"baseball", "id"=>1, "uid"=>"s:1", "leagues"=>[{"name"=>"Major League Baseball", "abbreviation"=>"mlb", "id"=>10, "uid"=>"s:1~l:10", "groupId"=>9, "shortName"=>"MLB", "teams"=>[{"id"=>17, "uid"=>"s:1~l:10~t:17", "location"=>"Cincinnati", "name"=>"Reds", "abbreviation"=>"CIN", "color"=>"D60042", "venues"=>[{"id"=>83, "name"=>"Great American Ball Park", "city"=>"Cincinnati", "state"=>"Ohio", "country"=>"", "capacity"=>0}], "links"=>{"api"=>{"teams"=>{"href"=>"http://api.espn.com/v1/sports/baseball/mlb/teams/17"}, "news"=>{"href"=>"http://api.espn.com/v1/sports/baseball/mlb/teams/17/news"}, "notes"=>{"href"=>"http://api.espn.com/v1/sports/baseball/mlb/teams/17/news/notes"}}, "web"=>{"teams"=>{"href"=>"http://espn.go.com/mlb/team/_/name/cin/cincinnati-reds?ex_cid=espnapi_public"}}, "mobile"=>{"teams"=>{"href"=>"http://m.espn.go.com/mlb/clubhouse?teamId=17&ex_cid=espnapi_public"}}}}]}]}]

但没有其他东西可以解析。请帮忙!

就像我说的,我不想做任何花哨的事情,只是为了好玩而想出一点 Ruby,但我已经被这个问题困住了好几天了。任何帮助,将不胜感激!

编辑:

直接来自 API 的 JSON:

{"sports" :[{"name" :"baseball","id" :1,"uid" :"s:1","leagues" :[{"name" :"Major League Baseball","abbreviation" :"mlb","id" :10,"uid" :"s:1~l:10","groupId" :9,"shortName" :"MLB","teams" :[{"id" :17,"uid" :"s:1~l:10~t:17","location" :"Cincinnati","name" :"Reds","abbreviation" :"CIN","color" :"D60042","venues" :[{"id" :83,"name" :"Great American Ball Park","city" :"Cincinnati","state" :"Ohio","country" :"","capacity" :0}],"links" :{"api" :{"teams" :{"href" :"http://api.espn.com/v1/sports/baseball/mlb/teams/17"},"news" :{"href" :"http://api.espn.com/v1/sports/baseball/mlb/teams/17/news"},"notes" :{"href" :"http://api.espn.com/v1/sports/baseball/mlb/teams/17/news/notes"}},"web" :{"teams" :{"href" :"http://espn.go.com/mlb/team/_/name/cin/cincinnati-reds?ex_cid=espnapi_public"}},"mobile" :{"teams" :{"href" :"http://m.espn.go.com/mlb/clubhouse?teamId=17&ex_cid=espnapi_public"}}}}]}]}],"resultsOffset" :0,"resultsLimit" :50,"resultsCount" :1,"timestamp" :"2013-08-05T19:44:32Z","status" :"success"}

data.inspect 的结果:

"{\"sports\" :[{\"name\" :\"baseball\",\"id\" :1,\"uid\" :\"s:1\",\"leagues\" :[{\"name\" :\"Major League Baseball\",\"abbreviation\" :\"mlb\",\"id\" :10,\"uid\" :\"s:1~l:10\",\"groupId\" :9,\"shortName\" :\"MLB\",\"teams\" :[{\"id\" :17,\"uid\" :\"s:1~l:10~t:17\",\"location\" :\"Cincinnati\",\"name\" :\"Reds\",\"abbreviation\" :\"CIN\",\"color\" :\"D60042\",\"venues\" :[{\"id\" :83,\"name\" :\"Great American Ball Park\",\"city\" :\"Cincinnati\",\"state\" :\"Ohio\",\"country\" :\"\",\"capacity\" :0}],\"links\" :{\"api\" :{\"teams\" :{\"href\" :\"http://api.espn.com/v1/sports/baseball/mlb/teams/17\"},\"news\" :{\"href\" :\"http://api.espn.com/v1/sports/baseball/mlb/teams/17/news\"},\"notes\" :{\"href\" :\"http://api.espn.com/v1/sports/baseball/mlb/teams/17/news/notes\"}},\"web\" :{\"teams\" :{\"href\" :\"http://espn.go.com/mlb/team/_/name/cin/cincinnati-reds?ex_cid=espnapi_public\"}},\"mobile\" :{\"teams\" :{\"href\" :\"http://m.espn.go.com/mlb/clubhouse?teamId=17&ex_cid=espnapi_public\"}}}}]}]}],\"resultsOffset\" :0,\"resultsLimit\" :50,\"resultsCount\" :1,\"timestamp\" :\"2013-08-05T19:44:24Z\",\"status\" :\"success\"}"
4

2 回答 2

1

parsed["sports"]不存在,解析您的输入并检查它/转储它

于 2013-08-03T14:41:31.210 回答
0

使用data您在问题中提供的信息,您可以获得如下venues信息:

require 'json'

json = JSON.parse data

json["sports"].first["leagues"].first["teams"].first["venues"]
# => [{"id"=>83, "name"=>"Great American Ball Park", "city"=>"Cincinnati", "state"=>"Ohio", "country"=>"", "capacity"=>0}]

通过用迭代器替换每个first调用,您可以在不知道数据在哪里的情况下进行搜索:

json["sports"].each{|h|
  h["leagues"].each{|h|
    h["teams"].each{|h|
      venues = h["venues"].map{|h| h["name"]}.join(", ")
      puts %Q!name: #{h["location"]} #{h["name"]} venues: #{venues}!
    }
  }
}

这输出:

名称:辛辛那提红人队 场地:Great American Ball Park

根据响应数据的稳定性,您可以删除几个迭代器:

json["sports"].first["leagues"]
              .first["teams"]
              .each{|h|
                venues = h["venues"].map{|h| h["name"] }.join(", ")
                puts %Q!name: #{h["location"]} #{h["name"]} venues: #{venues}!
              }

并且您很可能希望保存数据,因此类似each_with_object的内容很有帮助:

team_and_venues = json["sports"].first["leagues"]
              .first["teams"]
              .each_with_object([]){|h,xs|
                venues = h["venues"].map{|h| h["name"]}.join(", ")
                xs << %Q!name: #{h["location"]} #{h["name"]} venues: #{venues}!
              }
# => ["name: Cincinnati Reds venues: Great American Ball Park"]

team_and_venues
# => ["name: Cincinnati Reds venues: Great American Ball Park"]

请注意,当迭代器声明变量时,即使块外存在同名变量,块的范围也会受到尊重,并且块的变量仍然是本地的。

如果你问我,那是一些非常丑陋的代码,但这是一个开始的地方。

于 2013-08-05T20:37:25.930 回答