0

我正在使用 Koala gem 接收 Facebook 提要的 Hash 对象,但我遇到了问题。

返回的Hash对象如下:

{"post_id"=>"19292868552_356643851019175", "message"=>"Tune in tomorrow, January 11 at 12 PM PST / 3 PM EST / 8 PM GMT to join Facebook Live from the Consumer Electronics Show. We will be discussing some of the best social applications and gadgets with people who built them. Watch the video and share your questions on Facebook Live - facebook.com/facebooklive.", "attachment"=>{"media"=>[{"href"=>"http://facebook.com/facebooklive", "alt"=>"", "type"=>"link", "src"=>"http://external.ak.fbcdn.net/safe_image.php?d=AQCkZ2fRUjE2ofi5&w=90&h=90&url=http%3A%2F%2Fprofile.ak.fbcdn.net%2Fhprofile-ak-snc4%2F195784_150984694912422_7311456_n.jpg"}], "name"=>"Facebook Live", "href"=>"http://facebook.com/facebooklive", "caption"=>"www.facebook.com", "description"=>"Facebook Live is Facebook’s official live video streaming channel, provide a deeper look into our...", "properties"=>[], "icon"=>"http://static.ak.fbcdn.net/rsrc.php/v1/yD/r/aS8ecmYRys0.gif", "fb_object_type"=>""}}

现在,当我尝试访问 Hash 对象的链接时,如下所示:

<hash-object>["attachment]["media"][0]["href"]

我收到以下错误:

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

我尝试通过以下观察来调试错误:

<hash-object>["attachment]["media"][0].class # Hash (still it says undefined method[] for NilClass)

此外,当我在命令行中运行它时,它不会引发任何错误。我对这个问题感到困惑,因为它没有任何意义(即在命令行中工作而不是在 Sinatra 中工作)。

我尝试过的事情:

JSON.parse(<hash-object>.to_json) # to re-create the Hash Object

而且我能够在以下之前获取对象而不会出现任何错误:

post["attachment"]["media"][0] # it returns the output as it should be

提前致谢。

4

0 回答 0