سؤال

I have a hash from a JSON object. It looks like this:

JSON = {"kind"=>"calendar#freeBusy", "timeMin"=>"2012-02-19T19:35:00.000Z", "timeMax"=>"2012-02-19T19:40:00.000Z", "calendars"=>{"av3ddlgc54qe4brii4r7pius6k@group.calendar.google.com"=>{"busy"=>[{"start"=>"2012-02-19T19:35:00Z", "end"=>"2012-02-19T19:40:00Z"}]}}}

I want to check if there's any children of busy (a child of calendars).

I can access the children of calendars by JSON["calendars"]

That returns:

{"av3ddlgc54qe4brii4r7pius6k@group.calendar.google.com"=>{"busy"=>[{"start"=>"2012-02-19T19:35:00Z", "end"=>"2012-02-19T19:40:00Z"}]}}

but JSON["calendars"]["busy"] (which is how I think you're meant to access child elements?) returns nil.

How do I get inside the "busy" child?

I created the hash using JSON.parse to a Faraday request.

هل كانت مفيدة؟

المحلول

"busy" is nested under "av3ddlgc54qe4brii4r7pius6k@group.calendar.google.com" in your case, so you need to include this as well: JSON["calendars"]["av3ddlgc54qe4brii4r7pius6k@group.calendar.google.com"]["busy"].If you want access this attribute for all calendars, you'll have to loop:

JSON["calendars"].each do |key, value|
  # working with value["busy"]...
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top