0

私は Java の初心者で、Ruby は初めてです。私はそれが何ができるかを見るためだけに遊んでいましたが、解決できない問題に直面しています。

Sinatra で何ができるかを確認するために、もう一度 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"}

オブジェクトの会場部分、特に名前の値を取得したいと思います。解析しようとするたびに、「nil から文字列に変更できません」という行に沿ってエラーが発生し、整数から文字列へのエラーも発生します。

これが私がこれまでに持っているものです:

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

これは以下を出力します:

名前: シンシナティ レッズ会場: グレート アメリカン ボール パーク

応答データの安定性に応じて、いくつかの反復子を切り取ることができる場合があります。

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 に答える