2011-12-20 2 views
1

に変換するためにJSONと分割オブジェクトを解析I持って次のコード:すべてののRuby/Railsは - 日時

{"rss":{"version":"2.0","channel":{"title":"EnhanceTV Melbourne TV Guide ","description":null,"link":"http://www.enhancetv.com.au","lastBuildDate":"Wed, 21 Dec 
2011 10:25:55 1000","generator":"FeedCreator 1.7.2","item":[{"title":"Toasted TV - TEN - 07:00:00 - 21/12/2011","link":"http://www.enhancetv.com.au/tvguide/","d 
escription":"Join the team for the latest in gaming, sport, gadgets, pop culture, movies, music and other seriously fun stuff! Featuring a variety of your favou 
rite cartoons."},{"title":"Totally Wild - TEN - 08:00:00 - 21/12/2011","link":"http://www.enhancetv.com.au/tvguide/","description":"The Totally Wild team brings 
you the latest in action, adventure and wildlife from Australia and around theglobe."},{"title":"Explore: Africa's Rift Valley - SBS ONE - 19:30:00 - 21/ 
12/2011","link":"http://www.enhancetv.com.au/tvguide/","description":"Simon Reeve leads a team of journalists on a spectacular journey down East Africa's R 
ift Valley. From the tiny country of Djibouti, which is the centre of America's military presence in Africa, to the wide open plains of Kenya, the team enc 
ounter awe-inspiring landscapes, rich culture and amazing wildlife."}]}}} 

まず私がしたいのですが、次のJSONを出力

page = Net::HTTP.get_response(URI.parse('http://www.enhancetv.com.au/tvguide/rss/melbournerss.php')).body rescue nil 
show_info = Hash.from_xml(page).to_json 
puts show_info 

実際にはそれぞれの項目をループすることができます。

これは私がこれまで行ってきたことですが、エラーが発生しています。私は、その後にsplitを行うことができるようにしたいと思います

scraper.rb:118:in `<main>': undefined method `each' for "rss":String (NoMethodEr 
ror) 

:これは、次のエラーを与える

result = JSON.parse(show_info) # convert JSON data to native ruby 

result["channel"].each do |a| 
    puts a['title'] 
    puts a['description'] 
    puts a['link'] 
end 

:私はどのようにしてもループに解析されたJSONを適切全くわからないんだけど"title"ですので、後で使用するDateTimeオブジェクトに時間と日付を変換することができます。

ありがとうございます。

+0

'NoMethodError:未定義のメソッド' each 'for nil:NilClass'というエラーは、JSONの意味がわかります。そのエラーメッセージがコードとそのJSONから来たことは確かですか? –

+1

元のソースはxmlに入っているので、Nokogiriを使って構文解析を行い、内容の時間と時間を変換するのではなく、他の方法を使うのはどうですか? –

+0

@muistooshortええ、それは間違いなくその特定のコード行とJSONから来ています。 – fuzz

答えて

0

XMLをJSONに変換する代わりに、Nokogiriを使用してXMLを解析することにしました。

ありがとうございました。