无法从JSON响应中获取项目

时间:2016-03-26 12:25:21

标签: ruby-on-rails json api httparty

我目前正在使用HTTParty来获取API端点,当在控制台中测试它时,它似乎为nil提出了'undefined method` []':NilClass'。

这是我正在采取的步骤:

response = HTTParty.get("https://api.import.io/store/connector/ab589079-436a-47da-923c-c77cb572491d/_query?input=webpage/url:http%3A%2F%2Flinxspiration.com%2F%23_%3D_&&_apikey=dc9014801ce443229a302ec1db31bc7683360af6669d43767164345452e9faef39e958817471e8c77f894d1032293acc8656cefa3df03123ee06a331f313c7f131ed345f4bf0fa8f460a80a6e9c4cbc1")

返回

#<HTTParty::Response:0x5231220 parsed_response={"offset"=>0, "results"=>[{"tags_link"=>"http://linxspiration.com/tagged/women", "date_link_numbers/_source"=>"53", "media_images/_alt"=>"by Joakim Karlsson", "block_content"=>"by Joakim Karlsson", "date_link"=>"http://linxspiration.com/post/141713637264/by-joakim-karlsson", "media_images"=>"http://40.media.tumblr.com/22ef0cc5c89400be3346de71eaff7333/tumblr_o4lw3rLlhi1qkegsbo1_1280.jpg", "date_link_numbers"=>53.0, "likes_link_numbers"=>73.0, "blazepress_link"=>"http://t.umblr.com/redirect?z=https%3A%2F%2F500px.com%2Fjoakimkarlssonphotography&t=NjhiYmIxZDdmZTk2Y2Q1YmQ0ZjE5Nzg2ZjAxMjU1MWQwZmY5NjBhNCxkTFNrT2c2Tw%3D%3D", "likes_link/_title"=>"73 notes", "tags_link/_text"=>"#women", "likes_link/_text"=>"73 notes", "blazepress_link/_text"=>"Joakim Karlsson", "date_link/_text"=>"53 minutes ago", "media_link"=>"http://linxspiration.com/post/141713637264/by-joakim-karlsson", "likes_link_numbers/_source"=>"73", "date_link/_title"=>"53 minutes ago", "likes_link"=>"http://linxspiration.com/post/141713637264/by-joakim-karlsson#notes"}, {"date_link_numbers/_source"=>"2", "blockquote_content"=>"City Skylines. Buy this poster. linxsupply.com", "media_images/_alt"=>"linxsupply:\n\nCity Skylines.\nBuy this poster.\nlinxsupply.com", "block_content"=>"linxsupply:", "date_link"=>"http://linxspiration.com/post/141711949548/linxsupply-city-skylines-buy-this-poster", "media_images"=>"http://40.media.tumblr.com/adb42e7317096b5f2884314110c6594f/tumblr_o0mxowUg7e1uhpqwfo1_1280.jpg", "date_link_numbers"=>2.0, "content_link"=>["http://t.umblr.com/redirect?z=https%3A%2F%2Flinxsupply.com%2Fproduct%2Fcity-skylines%2F&t=NGE5M2YxZDRhYTkyNjIzOTFkYmMxZjg3YjUxZGY1YWE1MTg0MmVmNSxscnB6Y2p2cQ%3D%3D", "http://t.umblr.com/redirect?z=https%3A%2F%2Flinxsupply.com%2Fproduct%2Fcity-skylines%2F&t=NGE5M2YxZDRhYTkyNjIzOTFkYmMxZjg3YjUxZGY1YWE1MTg0MmVmNSxscnB6Y2p2cQ%3D%3D", "http://t.umblr.com/redirect?z=http%3A%2F%2Flinxsupply.com&t=ZDcxZDIwY2I2NDg5YzM0NjY1YjgxYmY1NWYxYTFhNDE0Y2EyN2Y4ZCxscnB6Y2p2cQ%3D%3D"], "likes_link_numbers"=>2108.0, "blazepress_link"=>"http://linxsupply.tumblr.com/post/136877431779", "likes_link/_title"=>"2,108 notes", "likes_link/_text"=>"2,108 notes", "blazepress_link/_text"=>"linxsupply", "date_link/_text"=>"2 hours ago", "media_link"=>"http://linxspiration.com/post/141711949548/linxsupply-city-skylines-buy-this-poster", "likes_link_numbers/_source"=>"2,108", "date_link/_title"=>"2 hours ago", "likes_link"=>"http://linxspiration.com/post/141711949548/linxsupply-city-skylines-buy-this-poster#notes", "content_link/_text"=>["City Skylines.", "Buy this poster.", "linxsupply.com"]}, {"date_link_numbers/_source"=>"3", "blockquote_content"=>"Linx Snapback Buy this hat linxsupply.com", "media_images/_alt"=>"linxsupply:\n\nLinx Snapback\nBuy this hat\nlinxsupply.com", "block_content"=>"linxsupply:", "date_link"=>"http://linxspiration.com/post/141710261817/linxsupply-linx-snapback-buy-this-hat", "media_images"=>"http://36.media.tumblr.com/09692725db89595d54cb7ad6e47e8cff/tumblr_o0pifybRpl1uhpqwfo1_1280.jpg", "date_link_numbers"=>3.0, "content_link"=>["http://t.umblr.com/redirect?z=https%3A%2F%2Flinxsupply.com%2Fproduct%2Flinx-outline-snapback%2F&t=Mjk1YTEyMzZhMzFkYzRjYjBkZjAzYmMwOGI1NWY5MTdjM2NhMWE1OCxDbGN2RThHbQ%3D%3D", "http://t.umblr.com/redirect?z=https%3A%2F%2Flinxsupply.com%2Fproduct%2Flinx-outline-snapback%2F&t=Mjk1YTEyMzZhMzFkYzRjYjBkZjAzYmMwOGI1NWY5MTdjM2NhMWE1OCxDbGN2RThHbQ%3D%3D", "http://t.umblr.com/redirect?z=http%3A%2F%2Flinxsupply.com&t=M2EwZDIyN2YwZWZlNGZhMGMwMGUzNDhkNzkyMGY5YWVjYWQ0MDdkMCxDbGN2RThHbQ%3D%3D"], "likes_link_numbers"=>86.0, "blazepress_link"=>"http://linxsupply.tumblr.com/post/136972374084", "likes_link/_title"=>"86 notes", "likes_link/_text"=>"86 notes", "blazepress_link/_text"=>"linxsupply", "date_link/_text"=>"3 hours ago", "media_link"=>"http://linxspiration.com/post/141710261817/linxsupply-linx-snapback-buy-this-hat", "likes_link_numbers/_source"=>"86", "date_link/_title"=>"3 hours ago", "likes_link"=>"http://linxspiration.com/post/141710261817/linxsupply-linx-snapback-buy-this-hat#notes", "content_link/_text"=>["Linx Snapback", "Buy this hat", "linxsupply.com"]}, {"date_link_numbers/_source"=>"4", "blockquote_content"=>"Buy This Poster linxsupply.com", "media_images/_alt"=>"linxsupply:\n\nBuy This Poster\nlinxsupply.com", "block_content"=>"linxsupply:", "date_link"=>"http://linxspiration.com/post/141708477302/linxsupply-buy-this-poster-linxsupplycom", "media_images"=>"http://36.media.tumblr.com/826c6605838904baae1395263ce4bff1/tumblr_o1enzn1amD1uhpqwfo1_1280.jpg", "date_link_numbers"=>4.0, "content_link"=>["http://t.umblr.com/redirect?z=https%3A%2F%2Flinxsupply.com%2Fproduct%2Fretro%2F&t=ZDJmYmFkMGRlN2RjNzgwZWMzYzAwZmJjZWRkMThhNmY3MjhlMTI4OSx2S3gyZ25GVA%3D%3D", "http://t.umblr.com/redirect?z=http%3A%2F%2Flinxsupply.com&t=ZTYxMzU5Yzg4ODAyYTk0MGY0ZmU5MjFkOTRlMmJiYWZjZTM3YzA2Yix2S3gyZ25GVA%3D%3D"], "likes_link_numbers"=>352.0, "blazepress_link"=>"http://linxsupply.tumblr.com/post/137874566104", "likes_link/_title"=>"352 notes", "likes_link/_text"=>"352 notes", "blazepress_link/_text"=>"linxsupply", "date_link/_text"=>"4 hours ago", "media_link"=>"http://linxspiration.com/post/141708477302/linxsupply-buy-this-poster-linxsupplycom", "likes_link_numbers/_source"=>"352", "date_link/_title"=>"4 hours ago", "likes_link"=>"http://linxspiration.com/post/141708477302/linxsupply-buy-this-poster-linxsupplycom#notes", "content_link/_text"=>["Buy This Poster", "linxsupply.com"]}, {"date_link_numbers/_source"=>"5", "blockquote_content"=>"Delicate Ink Drawings on a Miniature Scale by Christian Watson", "date_link/_text"=>"5 hours ago", "likes_link_numbers/_source"=>"303", "block_content"=>"blazepress:", "date_link"=>"http://linxspiration.com/post/141706441055/blazepress-delicate-ink-drawings-on-a-miniature", "date_link/_title"=>"5 hours ago", "media_images"=>["http://41.media.tumblr.com/373bf8dcf6f9b6add8059590d1af2faf/tumblr_o4i0wj8BVV1tq9q5vo1_500.jpg", "http://41.media.tumblr.com/db57dad8b359f2aa077bb1b839b7e2ec/tumblr_o4i0wj8BVV1tq9q5vo2_500.jpg", "http://41.media.tumblr.com/623bddbeb3d82f161fed8f947d365a95/tumblr_o4i0wj8BVV1tq9q5vo3_500.jpg"], "date_link_numbers"=>5.0, "content_link"=>"http://t.umblr.com/redirect?z=http%3A%2F%2Fblazepress.com%2F2016%2F03%2Fdelicate-ink-drawings-on-a-miniature-scale-by-christian-watson%2F&t=MWUzODY3MWYxZDU4OWEyNDhiYWE2NDUzZDk5N2ZlODFlNDdhMTkyMixqRDZ6dVFTRQ%3D%3D", "likes_link_numbers"=>303.0, "likes_link"=>"http://linxspiration.com/post/141706441055/blazepress-delicate-ink-drawings-on-a-miniature#notes", "blazepress_link"=>"http://blazepress.tumblr.com/post/141607191351", "likes_link/_title"=>"303 notes", "likes_link/_text"=>"303 notes", "content_link/_text"=>"Delicate Ink Drawings on a Miniature Scale by Christian Watson", "blazepress_link/_text"=>"blazepress"}, {"tags_link"=>"http://linxspiration.com/tagged/women", "date_link_numbers/_source"=>"6", "media_images/_alt"=>"by Stef Hartog", "block_content"=>"by Stef Hartog", "date_link"=>"http://linxspiration.com/post/141703981640/by-stef-hartog", "media_images"=>"http://36.media.tumblr.com/c843e71790a636a6e149d8c70df98223/tumblr_o4lvsoKZ301qkegsbo1_1280.png", "date_link_numbers"=>6.0, "likes_link_numbers"=>112.0, "blazepress_link"=>"http://t.umblr.com/redirect?z=https%3A%2F%2F500px.com%2Fphoto%2F136087669%2Fkim-and-calvin-by-stef-hartog%3Fctx_page%3D1%26from%3Duser%26user_id%3D6609624&t=ZDRjY2U0ZGQ2NmRjNzdkZGMzYTdjMmVmNjMzZDQ1NWQ4YjM3NzJjZCxXVHRtRVVCVw%3D%3D", "likes_link/_title"=>"112 notes", "tags_link/_text"=>"#women", "likes_link/_text"=>"112 notes", "blazepress_link/_text"=>"Stef Hartog", "date_link/_text"=>"6 hours ago", "media_link"=>"http://linxspiration.com/post/141703981640/by-stef-hartog", "likes_link_numbers/_source"=>"112", "date_link/_title"=>"6 hours ago", "likes_link"=>"http://linxspiration.com/post/141703981640/by-stef-hartog#notes"}, {"date_link_numbers/_source"=>"7", "blockquote_content"=>"Russia.", "media_images/_alt"=>"blazepress:\n\nRussia.", "block_content"=>"blazepress:", "date_link"=>"http://linxspiration.com/post/141701003183/blazepress-russia", "media_images"=>"http://41.media.tumblr.com/f183cfc29d82696d008bb2c0631bc75e/tumblr_o4k8lsBGAh1tq9q5vo1_1280.jpg", "date_link_numbers"=>7.0, "content_link"=>"http://t.umblr.com/redirect?z=http%3A%2F%2Fi.imgur.com%2F791rugC.jpg&t=NTA1MjYwMWUyMWNiMmZkOGMwMmU2ZGU4MTAzYzVmY2MwY2RlZDFmOCxLYlM4UVNDcw%3D%3D", "likes_link_numbers"=>178.0, "blazepress_link"=>"http://blazepress.tumblr.com/post/141625099144", "likes_link/_title"=>"178 notes", "likes_link/_text"=>"178 notes", "blazepress_link/_text"=>"blazepress", "date_link/_text"=>"7 hours ago", "media_link"=>"http://linxspiration.com/post/141701003183/blazepress-russia", "likes_link_numbers/_source"=>"178", "date_link/_title"=>"7 hours ago", "likes_link"=>"http://linxspiration.com/post/141701003183/blazepress-russia#notes", "content_link/_text"=>"Russia."}, {"date_link_numbers/_source"=>"8", "date_link/_text"=>"8 hours ago", "media_link"=>"http://linxspiration.com/post/141697715602", "likes_link_numbers/_source"=>"69", "date_link"=>"http://linxspiration.com/post/141697715602", "date_link/_title"=>"8 hours ago", "media_images"=>"http://41.media.tumblr.com/4cd79c44ceb0c297aa491475cb822039/tumblr_o4lvr0p5RD1qkegsbo1_1280.jpg", "date_link_numbers"=>8.0, "likes_link_numbers"=>69.0, "likes_link"=>"http://linxspiration.com/post/141697715602#notes", "likes_link/_title"=>"69 notes", "likes_link/_text"=>"69 notes"}, {"date_link_numbers/_source"=>"10", "date_link/_text"=>"10 hours ago", "media_link"=>"http://linxspiration.com/post/141694470674", "likes_link_numbers/_source"=>"228", "date_link"=>"http://linxspiration.com/post/141694470674", "date_link/_title"=>"10 hours ago", "media_images"=>"http://41.media.tumblr.com/cd42ef60f6e7222c96a6fc460a68116f/tumblr_o4lvpfZMfg1qkegsbo1_1280.jpg", "date_link_numbers"=>10.0, "likes_link_numbers"=>228.0, "likes_link"=>"http://linxspiration.com/post/141694470674#notes", "likes_link/_title"=>"228 notes", "likes_link/_text"=>"228 notes"}, {"tags_link"=>"http://linxspiration.com/tagged/landscape", "date_link_numbers/_source"=>"11", "media_images/_alt"=>"San Diego", "block_content"=>"San Diego", "date_link"=>"http://linxspiration.com/post/141691352399/san-diego", "media_images"=>"http://41.media.tumblr.com/497518d0508e5c833b780dec7e338a10/tumblr_o4lvleY3aB1qkegsbo1_1280.jpg", "date_link_numbers"=>11.0, "likes_link_numbers"=>108.0, "blazepress_link"=>"http://t.umblr.com/redirect?z=https%3A%2F%2F500px.com%2Fphoto%2F145836605%2Fsun-diego-by-kyle-kuiper%3Fctx_page%3D1%26from%3Duser%26user_id%3D13957979&t=NjQ0MTE5YzBjYjcwODExYjNlNWYxZWQ0ZDg2NTc5NDc0NDlkYjk5YSxpTHdTUTBocg%3D%3D", "likes_link/_title"=>"108 notes", "tags_link/_text"=>"#landscape", "likes_link/_text"=>"108 notes", "blazepress_link/_text"=>"San Diego", "date_link/_text"=>"11 hours ago", "media_link"=>"http://linxspiration.com/post/141691352399/san-diego", "likes_link_numbers/_source"=>"108", "date_link/_title"=>"11 hours ago", "likes_link"=>"http://linxspiration.com/post/141691352399/san-diego#notes"}, {"date_link_numbers/_source"=>"13", "date_link/_text"=>"13 hours ago", "media_link"=>"http://linxspiration.com/post/141685016105", "likes_link_numbers/_source"=>"102", "date_link"=>"http://linxspiration.com/post/141685016105", "date_link/_title"=>"13 hours ago", "media_images"=>"http://36.media.tumblr.com/d0bc7f18de3bba62fce33b9568ecbeab/tumblr_o4lvghfaCp1qkegsbo1_1280.jpg", "date_link_numbers"=>13.0, "likes_link_numbers"=>102.0, "likes_link"=>"http://linxspiration.com/post/141685016105#notes", "likes_link/_title"=>"102 notes", "likes_link/_text"=>"102 notes"}, {"date_link_numbers/_source"=>"14", "date_link/_text"=>"14 hours ago", "media_link"=>"http://linxspiration.com/post/141681640223", "likes_link_numbers/_source"=>"93", "date_link"=>"http://linxspiration.com/post/141681640223", "date_link/_title"=>"14 hours ago", "media_images"=>"http://41.media.tumblr.com/19577ca526da664fa5a3b3a49b87ada2/tumblr_o4lvfg6v1n1qkegsbo1_1280.jpg", "date_link_numbers"=>14.0, "likes_link_numbers"=>93.0, "likes_link"=>"http://linxspiration.com/post/141681640223#notes", "likes_link/_title"=>"93 notes", "likes_link/_text"=>"93 notes"}, {"tags_link"=>"http://linxspiration.com/tagged/women", "date_link_numbers/_source"=>"15", "media_images/_alt"=>"by Sean Archer", "block_content"=>"by Sean Archer", "date_link"=>"http://linxspiration.com/post/141678296075/by-sean-archer", "media_images"=>"http://41.media.tumblr.com/0a39d9ee6540da7bc86e80e0df88048c/tumblr_o4lvdoIiAo1qkegsbo1_1280.png", "date_link_numbers"=>15.0, "likes_link_numbers"=>130.0, "blazepress_link"=>"http://t.umblr.com/redirect?z=https%3A%2F%2F500px.com%2Fphoto%2F123406077%2Fanna-by-sean-archer%3Fctx_page%3D2%26from%3Duser%26user_id%3D777395&t=NGU3MmYwOTM3ODFkZDhiNWVlYjIxOTJmNmVjZDgzOWEyMjhjYjRjYyxTY3Mzdk90TA%3D%3D", "likes_link/_title"=>"130 notes", "tags_link/_text"=>"#women", "likes_link/_text"=>"130 notes", "blazepress_link/_text"=>"Sean Archer", "date_link/_text"=>"15 hours ago", "media_link"=>"http://linxspiration.com/post/141678296075/by-sean-archer", "likes_link_numbers/_source"=>"130", "date_link/_title"=>"15 hours ago", "likes_link"=>"http://linxspiration.com/post/141678296075/by-sean-archer#notes"}, {"date_link_numbers/_source"=>"16", "blockquote_content"=>"Hidden town, Monemvasia, Greece.", "media_images/_alt"=>"blazepress:\n\nHidden town, Monemvasia, Greece.", "block_content"=>"blazepress:", "date_link"=>"http://linxspiration.com/post/141675015276/blazepress-hidden-town-monemvasia-greece", "media_images"=>"http://41.media.tumblr.com/2a0d94dcc910d6e2ba9c1524279af560/tumblr_o4k8p3P4N41tq9q5vo1_1280.jpg", "date_link_numbers"=>16.0, "likes_link_numbers"=>574.0, "blazepress_link"=>"http://blazepress.tumblr.com/post/141627814665", "likes_link/_title"=>"574 notes", "likes_link/_text"=>"574 notes", "blazepress_link/_text"=>"blazepress", "date_link/_text"=>"16 hours ago", "media_link"=>"http://linxspiration.com/post/141675015276/blazepress-hidden-town-monemvasia-greece", "likes_link_numbers/_source"=>"574", "date_link/_title"=>"16 hours ago", "likes_link"=>"http://linxspiration.com/post/141675015276/blazepress-hidden-town-monemvasia-greece#notes"}, {"date_link_numbers/_source"=>"18", "blockquote_content"=>"A Coffee Table so Cool You’ll Immediately Want One in Your Own Home", "date_link/_text"=>"18 hours ago", "likes_link_numbers/_source"=>"395", "block_content"=>"blazepress:", "date_link"=>"http://linxspiration.com/post/141671795034/blazepress-a-coffee-table-so-cool-youll", "date_link/_title"=>"18 hours ago", "media_images"=>["http://41.media.tumblr.com/57fc5fb22a35bb7f7be48e5f80e61ec8/tumblr_o4k98zdE3v1tq9q5vo1_500.jpg", "http://41.media.tumblr.com/240d4cf9ff2cd27b2e617c402bd06b24/tumblr_o4k98zdE3v1tq9q5vo2_500.jpg", "http://36.media.tumblr.com/c42f6d0c4c4b6d4cf0fcd30092d9bde7/tumblr_o4k98zdE3v1tq9q5vo3_500.jpg"], "date_link_numbers"=>18.0, "content_link"=>"http://t.umblr.com/redirect?z=http%3A%2F%2Fblazepress.com%2F2016%2F03%2Fa-coffee-table-so-cool-youll-immediately-want-one-in-your-own-home%2F&t=NDM5NDlmZDBhNDllNWRlYjcyYWUwYzdkOGM2NzZmMTUyNjg1OTk0NyxldldQSWpuRA%3D%3D", "likes_link_numbers"=>395.0, "likes_link"=>"http://linxspiration.com/post/141671795034/blazepress-a-coffee-table-so-cool-youll#notes", "blazepress_link"=>"http://blazepress.tumblr.com/post/141640731260", "likes_link/_title"=>"395 notes", "likes_link/_text"=>"395 notes", "content_link/_text"=>"A Coffee Table so Cool You’ll Immediately Want One in Your Own Home", "blazepress_link/_text"=>"blazepress"}], "cookies"=>[], "connectorVersionGuid"=>"84aaf0b4-2bed-5469-f8ff-a7e8c0467082", "connectorGuid"=>"ab589079-436a-47da-923c-c77cb572491d", "pageUrl"=>"http://linxspiration.com/#_=_", "outputProperties"=>[{"name"=>"media_images", "type"=>"IMAGE"}, {"name"=>"media_link", "type"=>"URL"}, {"name"=>"block_content", "type"=>"STRING"}, {"name"=>"blazepress_link", "type"=>"URL"}, {"name"=>"blockquote_content", "type"=>"STRING"}, {"name"=>"content_link", "type"=>"URL"}, {"name"=>"tags_link", "type"=>"URL"}, {"name"=>"date_link", "type"=>"URL"}, {"name"=>"date_link_numbers", "type"=>"DOUBLE"}, {"name"=>"likes_link", "type"=>"URL"}, {"name"=>"likes_link_numbers", "type"=>"DOUBLE"}]}, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"content-type"=>["application/json; charset=UTF-8"], "date"=>["Sat, 26 Mar 2016 12:20:14 GMT"], "server"=>["openresty/1.9.7.3"], "vary"=>["Accept-Encoding, User-Agent"], "content-length"=>["3615"], "connection"=>["Close"]}>

然后我尝试在这个解析后的数组中调用一个元素,如下所示:

puts response[0]["media_images"]

返回

NoMethodError:nil的未定义方法`[]':NilClass

1 个答案:

答案 0 :(得分:1)

要访问元素数组,请执行以下操作:

elements = response.parsed_response["results"]

然后你可以像这样访问第一个元素:

puts elements[0]["media_images"]