Ruby on rails RoR:输出哈希,获取正确的参数?

Ruby on rails RoR:输出哈希,获取正确的参数?,ruby-on-rails,ruby,ruby-on-rails-3,hash,Ruby On Rails,Ruby,Ruby On Rails 3,Hash,我的输出如下: #<Hashie::Mash created_time="1366008641" from=#<Hashie::Mash full_name="Cor Valen" id="22340" username="_corin"> id="4344344286" text="Look Who It Is, My Brother, My Favorite Deputy"> 我不明白 谢谢我想媒体就是这个对象。标题是什么?我在你的对象中没有看到这样的参数,那么

我的输出如下:

#<Hashie::Mash created_time="1366008641" 
from=#<Hashie::Mash full_name="Cor Valen" id="22340" username="_corin"> 
id="4344344286" text="Look Who It Is, My Brother, My Favorite Deputy">
我不明白


谢谢

我想媒体就是这个对象。标题是什么?我在你的对象中没有看到这样的参数,那么你为什么认为它存在呢?很明显,它为nil:NilClass抛出了未定义的方法“[…]”,因为您的属性不存在


您可能正在查找
media.from.text
,而不是
media.caption.text
我从您提供的链接复制了JSON并通过
irb
运行,请查看输出:

irb(main):004:0> media = Hashie::Mash.new(JSON.parse(File.open("inst.json").read))
=> #<Hashie::Mash data=[#<Hashie::Mash caption=#<Hashie::Mash created_time="1296656006" from=#<Hashie::Mash full_name="" id="1127272" type="user" username="cocomiin"> id="26329105" text="This is dummy text."> comments=#<Hashie::Mash> created_time="1296655883" filter="Gotham" id="22518783" images=#<Hashie::Mash low_resolution=#<Hashie::Mash height=306 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg" width=306> standard_resolution=#<Hashie::Mash height=612 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg" width=612> thumbnail=#<Hashie::Mash height=150 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg" width=150>> likes=#<Hashie::Mash count=35 data=[#<Hashie::Mash full_name="Kevin S" id="4" profile_picture="" username="mikeyk">, #<Hashie::Mash>]> link="http://instagr.am/p/BV5v_/" location=nil tags=[] type="image" user=#<Hashie::Mash full_name="Cocomiin" id="1127272" profile_picture="http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg" username="cocomiin">>]>
irb(main):005:0> media = media.data[0]
=> #<Hashie::Mash caption=#<Hashie::Mash created_time="1296656006" from=#<Hashie::Mash full_name="" id="1127272" type="user" username="cocomiin"> id="26329105" text="This is dummy text."> comments=#<Hashie::Mash> created_time="1296655883" filter="Gotham" id="22518783" images=#<Hashie::Mash low_resolution=#<Hashie::Mash height=306 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg" width=306> standard_resolution=#<Hashie::Mash height=612 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg" width=612> thumbnail=#<Hashie::Mash height=150 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg" width=150>> likes=#<Hashie::Mash count=35 data=[#<Hashie::Mash full_name="Kevin S" id="4" profile_picture="" username="mikeyk">, #<Hashie::Mash>]> link="http://instagr.am/p/BV5v_/" location=nil tags=[] type="image" user=#<Hashie::Mash full_name="Cocomiin" id="1127272" profile_picture="http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg" username="cocomiin">>
irb(main):006:0> media.caption.text
=> "This is dummy text."
irb(main):004:0>media=Hashie::Mash.new(JSON.parse(File.open(“inst.JSON”).read))
=> #
irb(主):005:0>media=media.data[0]
=> #
irb(主):006:0>media.caption.text
=>“这是虚拟文本。”

我在Instagram订阅源中遇到了这个问题,我发现某些媒体项目没有标题,所以抛出了错误

我用以下方法解决了这个问题:

<% unless media.caption.blank? %>
  <%= media.caption.text %>
<% end %>


仔细查看输出,“text”属性不是属性“from”所指对象的一部分,因此
媒体。from.text
也不起作用。哦,对我来说可能太早了。我没注意到这个收尾标签。。。所以
media.text
那么,由于这个属性属于main对象,我得到了那个散列错误,因为我使用的是media.caption。所以我假设散列错误是输出标题中的内容?但如果不是这样,那么我会尝试media.text。但是这没有意义,因为我试图根据
/media/popular
这里的响应来找出:
字幕
在数据json中的位置,即
媒体。字幕
?提供传递给Hashie的哈希::Mash@Anand什么意思?我试图获取文本,但是当使用caption时,我得到了散列输出。我的意思是,当使用media.caption时,您得到的是一个Hashie::Mash对象,您的代码的哪一部分在处理它?Instagram以JSON格式传递数据,解析后返回一个普通的散列对象。这太奇怪了。我是说media.caption.text应该有用。但我得到的方法文本为零。阿格!
irb(main):004:0> media = Hashie::Mash.new(JSON.parse(File.open("inst.json").read))
=> #<Hashie::Mash data=[#<Hashie::Mash caption=#<Hashie::Mash created_time="1296656006" from=#<Hashie::Mash full_name="" id="1127272" type="user" username="cocomiin"> id="26329105" text="This is dummy text."> comments=#<Hashie::Mash> created_time="1296655883" filter="Gotham" id="22518783" images=#<Hashie::Mash low_resolution=#<Hashie::Mash height=306 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg" width=306> standard_resolution=#<Hashie::Mash height=612 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg" width=612> thumbnail=#<Hashie::Mash height=150 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg" width=150>> likes=#<Hashie::Mash count=35 data=[#<Hashie::Mash full_name="Kevin S" id="4" profile_picture="" username="mikeyk">, #<Hashie::Mash>]> link="http://instagr.am/p/BV5v_/" location=nil tags=[] type="image" user=#<Hashie::Mash full_name="Cocomiin" id="1127272" profile_picture="http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg" username="cocomiin">>]>
irb(main):005:0> media = media.data[0]
=> #<Hashie::Mash caption=#<Hashie::Mash created_time="1296656006" from=#<Hashie::Mash full_name="" id="1127272" type="user" username="cocomiin"> id="26329105" text="This is dummy text."> comments=#<Hashie::Mash> created_time="1296655883" filter="Gotham" id="22518783" images=#<Hashie::Mash low_resolution=#<Hashie::Mash height=306 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg" width=306> standard_resolution=#<Hashie::Mash height=612 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg" width=612> thumbnail=#<Hashie::Mash height=150 url="http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg" width=150>> likes=#<Hashie::Mash count=35 data=[#<Hashie::Mash full_name="Kevin S" id="4" profile_picture="" username="mikeyk">, #<Hashie::Mash>]> link="http://instagr.am/p/BV5v_/" location=nil tags=[] type="image" user=#<Hashie::Mash full_name="Cocomiin" id="1127272" profile_picture="http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg" username="cocomiin">>
irb(main):006:0> media.caption.text
=> "This is dummy text."
<% unless media.caption.blank? %>
  <%= media.caption.text %>
<% end %>