在 Ruby 中解析来自 Eventbrite API 的 JSON 响应

Parse JSON response from Eventbrite API in Ruby

我正在尝试解析 Ruby 中 Eventbrite API 的 JSON 响应,但遇到了问题。具体来说,JSON 不处理嵌套的散列,这些散列随后显示为包含单个字符串的数组。这是我的脚本:

  require 'eventbrite' 
  require 'csv'
  require 'uri'
  require 'json' 
  require 'rest-client'

  response = RestClient.get 'https://www.eventbriteapi.com/v3/events/search/?organizer.id=4705752287&token=mytoken''

 response = JSON.parse(response)
`enter code here`puts response #this returns a ruby hash

当我打印出这个散列时,这是我得到的(如下)。我需要的是 "event" 键中的 "name" (文本格式)和 "description" (文本格式),目前在数组中的单个字符串中无法访问它们。请指教!我是 APIs 和解析 JSON 的新手。谢谢!

这是我得到的结果:

{"pagination"=>{"object_count"=>1, "page_number"=>1, "page_size"=>50, "page_count"=>1}, "events"=>[{"name"=>{"text"=>"Feb Fest Workshops @ the MIT Museum for Middle School Students", "html"=>"Feb Fest Workshops @ the MIT Museum for Middle School Students"}, "description"=>{"text"=>"  \nMiddle school students are invited to spend one day or the entire week at the MIT Museum and explore such topics as holography, physics & photobraphy, robotics, DNA, and structural engineering. Our educational team will help you think about developments that MIT researchers are making in these subjects. Each day will include two 2-hour Museum workshops, a gallery tour, and an on-campus field trip.  \nThis opportunity is limited to 20 middle school (6th - 8th grade) students per day. Participants should bring a bag lunch that does not require refrigeration and their signed liability form.  Individual students only; no groups please. Students may be brought to the Museum no earlier than 9:30 a.m. and must be picked up by 3:45 p.m.  \nEvery year, during the Massachusetts school vacation week in February, the MIT Museum celebrates National Engineers Week with engineering-specific activitiites, workshop, and demonstrations led by MIT students.  \n  \n  \n  ", "html"=>"<P><SPAN STYLE=\"color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Lucida, Helvetica, sans-serif; font-size: 13px; line-height: 20.479999542236328px;\"><BR></SPAN></P>\r\n<P><SPAN STYLE=\"color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Lucida, Helvetica, sans-serif; font-size: 13px; line-height: 20.479999542236328px;\">Middle school students are invited to spend one day or the entire week at the MIT Museum and explore such topics as holography, physics &amp; photobraphy, robotics, DNA, and structural engineering. Our educational team will help you think about developments that MIT researchers are making in these subjects. Each day will include two 2-hour <A HREF=\"http://web.mit.edu/museum/education/workshops.html\" REL=\"nofollow\">Museum workshops</A>, a gallery tour, and an on-campus field trip. </SPAN></P>\r\n<P><SPAN STYLE=\"color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Lucida, Helvetica, sans-serif; font-size: 13px; line-height: 20.479999542236328px;\"><STRONG>This opportunity is limited to 20 middle school (6th - 8th grade) students per day. Participants should bring a bag lunch that does not require refrigeration and their <A HREF=\"http://web.mit.edu/museum/education/pdfs/FEBFest%20Waiver%202016.pdf\" REL=\"nofollow\">signed liability form.</A>  Individual students only; no groups please. Students may be brought to the Museum no earlier than 9:30 a.m. and must be picked up by 3:45 p.m.</STRONG><BR></SPAN></P>\r\n<P><SPAN STYLE=\"color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Lucida, Helvetica, sans-serif; font-size: 13px; line-height: 20.479999542236328px;\">Every year, during the Massachusetts school vacation week in February, the MIT Museum celebrates National Engineers Week with engineering-specific activitiites, workshop, and demonstrations led by MIT students. </SPAN></P>\r\n<P> </P>\r\n<P> </P>\r\n<P> </P>"}, "id"=>"20514138340", "url"=>"http://www.eventbrite.com/e/feb-fest-workshops-the-mit-museum-for-middle-school-students-tickets-20514138340?aff=ebapi", "start"=>{"timezone"=>"America/New_York", "local"=>"2016-02-16T09:30:00", "utc"=>"2016-02-16T14:30:00Z"}, "end"=>{"timezone"=>"America/New_York", "local"=>"2016-02-19T15:30:00", "utc"=>"2016-02-19T20:30:00Z"}, "created"=>"2016-01-06T15:30:44Z", "changed"=>"2016-01-17T19:00:20Z", "capacity"=>80, "status"=>"live", "currency"=>"USD", "listed"=>true, "shareable"=>true, "online_event"=>false, "tx_time_limit"=>900, "hide_start_date"=>false, "locale"=>"en_US", "is_locked"=>false, "privacy_setting"=>"unlocked", "logo_id"=>"17744153", "organizer_id"=>"4705752287", "venue_id"=>"5023762", "category_id"=>"115", "subcategory_id"=>"15001", "format_id"=>nil, "resource_uri"=>"https://www.eventbriteapi.com/v3/events/20514138340/", "logo"=>{"id"=>"17744153", "url"=>"https://img.evbuc.com/https%3A%2F%2Fimg.evbuc.com%2Fhttp%253A%252F%252Fcdn.evbuc.com%252Fimages%252F17744153%252F84178497408%252F1%252Foriginal.jpg%3Frect%3D0%252C12%252C274%252C137%26s%3Dc0e0d68a95d928599ac5c33d1e115a76?h=200&w=450&s=9fc9f7a1f911240daa25bf3b6ad7c77f", "aspect_ratio"=>"2", "edge_color"=>"#ffffff", "edge_color_set"=>true}}]}

到目前为止,您发布的内容非常好!我认为您正在寻找的是访问值的查询(name-text 和 description-text)。

根据我的经验,(已解析 JSON)API 响应几乎总是由许多(嵌套的)散列和(嵌套的)数组组成。您可以通过提供 key-values 来导航这些散列,也可以通过提供 index-values 来导航数组。您还可以迭代任何(子)哈希/(子)数组。

所以,给定:

response = {"pagination"=>{"object_count"=>1, "page_number"=>1, "page_size"=>50, "page_count"=>1}, "events"=>[{"name"=>{"text"=>"Feb Fest Workshops @ the MIT Museum for Middle School Students", "html"=>"Feb Fest Workshops @ the MIT Museum for Middle School Students"}, "description"=>{"text"=>"  \nMiddle school students are invited to spend one day or the entire week at the MIT Museum and explore such topics as holography, physics & photobraphy, robotics, DNA, and structural engineering. Our educational team will help you think about developments that MIT researchers are making in these subjects. Each day will include two 2-hour Museum workshops, a gallery tour, and an on-campus field trip.  \nThis opportunity is limited to 20 middle school (6th - 8th grade) students per day. Participants should bring a bag lunch that does not require refrigeration and their signed liability form.  Individual students only; no groups please. Students may be brought to the Museum no earlier than 9:30 a.m. and must be picked up by 3:45 p.m.  \nEvery year, during the Massachusetts school vacation week in February, the MIT Museum celebrates National Engineers Week with engineering-specific activitiites, workshop, and demonstrations led by MIT students.  \n  \n  \n  ", "html"=>"<P><SPAN STYLE=\"color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Lucida, Helvetica, sans-serif; font-size: 13px; line-height: 20.479999542236328px;\"><BR></SPAN></P>\r\n<P><SPAN STYLE=\"color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Lucida, Helvetica, sans-serif; font-size: 13px; line-height: 20.479999542236328px;\">Middle school students are invited to spend one day or the entire week at the MIT Museum and explore such topics as holography, physics &amp; photobraphy, robotics, DNA, and structural engineering. Our educational team will help you think about developments that MIT researchers are making in these subjects. Each day will include two 2-hour <A HREF=\"http://web.mit.edu/museum/education/workshops.html\" REL=\"nofollow\">Museum workshops</A>, a gallery tour, and an on-campus field trip. </SPAN></P>\r\n<P><SPAN STYLE=\"color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Lucida, Helvetica, sans-serif; font-size: 13px; line-height: 20.479999542236328px;\"><STRONG>This opportunity is limited to 20 middle school (6th - 8th grade) students per day. Participants should bring a bag lunch that does not require refrigeration and their <A HREF=\"http://web.mit.edu/museum/education/pdfs/FEBFest%20Waiver%202016.pdf\" REL=\"nofollow\">signed liability form.</A>  Individual students only; no groups please. Students may be brought to the Museum no earlier than 9:30 a.m. and must be picked up by 3:45 p.m.</STRONG><BR></SPAN></P>\r\n<P><SPAN STYLE=\"color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Lucida, Helvetica, sans-serif; font-size: 13px; line-height: 20.479999542236328px;\">Every year, during the Massachusetts school vacation week in February, the MIT Museum celebrates National Engineers Week with engineering-specific activitiites, workshop, and demonstrations led by MIT students. </SPAN></P>\r\n<P> </P>\r\n<P> </P>\r\n<P> </P>"}, "id"=>"20514138340", "url"=>"http://www.eventbrite.com/e/feb-fest-workshops-the-mit-museum-for-middle-school-students-tickets-20514138340?aff=ebapi", "start"=>{"timezone"=>"America/New_York", "local"=>"2016-02-16T09:30:00", "utc"=>"2016-02-16T14:30:00Z"}, "end"=>{"timezone"=>"America/New_York", "local"=>"2016-02-19T15:30:00", "utc"=>"2016-02-19T20:30:00Z"}, "created"=>"2016-01-06T15:30:44Z", "changed"=>"2016-01-17T19:00:20Z", "capacity"=>80, "status"=>"live", "currency"=>"USD", "listed"=>true, "shareable"=>true, "online_event"=>false, "tx_time_limit"=>900, "hide_start_date"=>false, "locale"=>"en_US", "is_locked"=>false, "privacy_setting"=>"unlocked", "logo_id"=>"17744153", "organizer_id"=>"4705752287", "venue_id"=>"5023762", "category_id"=>"115", "subcategory_id"=>"15001", "format_id"=>nil, "resource_uri"=>"https://www.eventbriteapi.com/v3/events/20514138340/", "logo"=>{"id"=>"17744153", "url"=>"https://img.evbuc.com/https%3A%2F%2Fimg.evbuc.com%2Fhttp%253A%252F%252Fcdn.evbuc.com%252Fimages%252F17744153%252F84178497408%252F1%252Foriginal.jpg%3Frect%3D0%252C12%252C274%252C137%26s%3Dc0e0d68a95d928599ac5c33d1e115a76?h=200&w=450&s=9fc9f7a1f911240daa25bf3b6ad7c77f", "aspect_ratio"=>"2", "edge_color"=>"#ffffff", "edge_color_set"=>true}}]}

要return你想要的值,你可以使用这样的查询:

姓名文字:

response["events"][0]["name"]["text"] 要么 response["events"].first["name"]["text"]

描述文字:

response["events"].first["description"]["text"]

我弄清楚了嵌套级别是什么以及如何使用 gem awesome_print 查询它们,它转换任何 single-line hash/array/json 正确地 嵌套 一个。

要使用:在终端中执行 gem install awesome_print,然后在 file/irb/console 中使用 require 'awesome_print' 要求它并调用 ap 而不是 printputs,像这样:ap response.

希望对您有所帮助!