Ruby on rails Ruby/Rails-解析JSON并拆分对象以转换为datetime

Ruby on rails Ruby/Rails-解析JSON并拆分对象以转换为datetime,ruby-on-rails,ruby,json,datetime,split,Ruby On Rails,Ruby,Json,Datetime,Split,我有以下代码: 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 它输出以下JSON: {"rss":{"version":"2.0","channel":{"title":"EnhanceTV Melb

我有以下代码:

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
它输出以下JSON:

{"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:

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
这会产生以下错误:

scraper.rb:118:in `<main>': undefined method `each' for "rss":String (NoMethodEr
ror)
scraper.rb:118:in`':未定义“rss”的方法`each':String(NoMethodEr
(ror)
然后,我希望能够对
标题“
执行
拆分
,以便将时间和日期转换为
日期时间
对象,以便以后使用


提前感谢。

我决定使用Nokogiri解析XML,而不是将XML转换为JSON


感谢您的帮助。

A
NoMethodError:undefined方法
each'for nil:NilClass`error对我来说对JSON更有意义。您确定错误消息来自代码和JSON吗?因为原始源代码是xml,那么使用Nokogiri进行解析和其他操作,而不是一次又一次地转换内容怎么样。@muistooshort是的,“它肯定来自于特定的代码行和JSON。@Kassymorsel将看一看Nokogiri。您已经将XML转换为哈希结构。为什么要通过JSON进行往返?既然rss是散列中的顶级元素,那么它不应该是result[“rss”][“channel”]吗?