从Koala返回的Hash对象的奇怪问题

时间:2012-04-22 07:07:56

标签: facebook-graph-api hash sinatra koala

我正在使用Koala gem接收facebook feed的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

提前致谢。

0 个答案:

没有答案
相关问题