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

Posted

技术标签:

【中文标题】在 Ruby 中解析来自 Eventbrite API 的 JSON 响应【英文标题】:Parse JSON response from Eventbrite API in Ruby 【发布时间】:2016-01-18 16:53:00 【问题描述】:

我正在尝试解析来自 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

当我打印出这个哈希时,这就是我得到的(下)。我需要的是“事件”键中的“名称”(文本格式)和“描述”(文本格式),目前在数组中的单个字符串中无法访问它们。请指教!我是 API 和解析 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]

【问题讨论】:

您能否发布从 API 收到的原始 JSON,而不是强制我们注册 eventbrite 开发人员 API? 只需删除选项,因为您要求对其进行符号化。 response = JSON.parse(response) @WandMaker 我在上面发布了原始 JSON。我认为以前的代码也有你需要的。感谢您的关注。 @sidegeeks 添加选项(并删除它)对问题没有任何影响。谢谢。 【参考方案1】:

到目前为止,您发布的内容非常好!我认为您正在寻找的是访问值(名称文本和描述文本)的查询。

根据我的经验,(解析的 JSON)API 响应几乎总是由许多(嵌套)哈希和(嵌套)数组组成。您可以通过提供键值来导航这些哈希,并且可以通过提供索引值来导航数组。您还可以遍历任何(子)哈希/(子)数组。

所以,给定:

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]

要返回您想要的值,您可以使用如下查询:

名称文字:

response["events"][0]["name"]["text"] 或者 response["events"].first["name"]["text"]

说明文字:

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

我弄清楚了嵌套的级别以及如何使用 gem awesome_print 来查询它们,它将任何 单行 hash/array/json 转换为正确的 嵌套一个。

要使用:在终端中执行gem install awesome_print,然后在您的文件/irb/console 中使用require 'awesome_print' 调用它并调用ap 而不是printputs,如下所示:ap response

希望对您有所帮助!

【讨论】:

非常感谢@Glenn,做到了!感谢您的详细回复和 awesome_print gem。

以上是关于在 Ruby 中解析来自 Eventbrite API 的 JSON 响应的主要内容,如果未能解决你的问题,请参考以下文章

在 Ruby/Rails 中解析 A​​tom 和 RSS?

Eventbrite 重定向 URI iOS OAuthSwift

如何通过eventbrite API将事件包含在事件中

ruby编程中异常处理的实践解析

使用 Nokogiri xpath 解析时未插入 Ruby 环境变量

如何在Ruby中使用来自哈希的查询参数构造URI