Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何有效地从JSON中提取特定信息_Ruby_Json_Calendar_Google Calendar Api - Fatal编程技术网

Ruby 如何有效地从JSON中提取特定信息

Ruby 如何有效地从JSON中提取特定信息,ruby,json,calendar,google-calendar-api,Ruby,Json,Calendar,Google Calendar Api,我目前有一个公共谷歌日历,我正在使用它成功地向下拉JSON数据 我正在使用HTTParty将JSON转换为ruby对象 response = HTTParty.get('http://www.google.com/calendar/feeds/colorado.edu_mdpltf14q21hhg50qb3e139fjg@group.calendar.google.com/public/full?alt=json&orderby=starttime&max-results=15&

我目前有一个公共谷歌日历,我正在使用它成功地向下拉JSON数据

我正在使用HTTParty将JSON转换为ruby对象

response = HTTParty.get('http://www.google.com/calendar/feeds/colorado.edu_mdpltf14q21hhg50qb3e139fjg@group.calendar.google.com/public/full?alt=json&orderby=starttime&max-results=15&singleevents=true&sortorder=ascending&futureevents=true')
我想检索许多标题、事件名称、开始时间、结束时间等。我可以通过如下命令获得这些

response["feed"]["title"["$t"]
日历的标题,以及

response["feed"]["entry"][0]["title"]["$t"]
为活动的标题

我的问题有两个方面。第一,有没有更简单的方法来提取这些数据?第二,如何获取多个事件信息?我试过:

response.each do |x| response["feed"]["title"]["$t"]

但这会导致将字符串转换为整数的非隐式错误。

根据您的示例,这应该可以做到

 response["feed"]["entry"].map {|entry| entry["title"]["$t"] }

response['feed']['entry']
是一个简单的哈希数组。最好是使用

entries = response['feed']['entry']
此后,您的代码将完全取决于您需要实现什么。例如,使用您提供的URL

puts entries.length
显示

给予


如果我们可以帮助您实现某些特定的目标,请更改您的答案或在评论中要求澄清。

具体来说,我想创建一个数组。每个数组都应该具有与每个事件相关的所有数据。因此,数组[0][0]可能是第一个事件的标题,而数组[1][0]可能是第二个事件的标题。我想要标题、事件名称、开始时间、结束时间、位置和描述。
2
entries.each do |entry|
  puts entry['title']['$t']
end
NEW EVENT
Future EVENT