我正在尝试从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”(文本格式),它们当前在数组内的单个字符串中是不可访问的。请指教!我是API和Parsing 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 & 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}}]}
答案 0 :(得分:0)
到目前为止,您发布的内容非常好!我认为您正在寻找的是访问值的查询(名称 - 文本和描述 - 文本)。
根据我的经验,(解析的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 & 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查询它们,它将任何单行哈希/数组/ json转换为正确< em>嵌套一个。
要使用:在终端中执行gem install awesome_print
,然后使用require 'awesome_print'
在文件/ irb / console中输入,并拨打ap
而不是print
或{{1} },像这样:puts
。
希望它有所帮助!