Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Jbuilder使用Jbuilder对象提取json哈希结果?_Ruby On Rails_Json_Ruby_Jbuilder - Fatal编程技术网

Ruby on rails Jbuilder使用Jbuilder对象提取json哈希结果?

Ruby on rails Jbuilder使用Jbuilder对象提取json哈希结果?,ruby-on-rails,json,ruby,jbuilder,Ruby On Rails,Json,Ruby,Jbuilder,我试图使用Jbuilder从json数组中提取一些json对象,如下所示 irb> photos = [{"album"=>{"created_time"=>"123", "name"=>"hello"}, {"album"=>{"created_time"=>"123", "name"=>"hello2"},........] irb> json = Jbuilder.new irb> json.photos photos do |pho

我试图使用Jbuilder从json数组中提取一些json对象,如下所示

irb> photos = [{"album"=>{"created_time"=>"123", "name"=>"hello"}, {"album"=>{"created_time"=>"123", "name"=>"hello2"},........]

irb> json = Jbuilder.new
irb> json.photos photos do |photo|
irb* json.created_time photo.album.created_time
irb* end
nomethoderror: undefined method 'album' for Hash:0x65...>
或者使用
转换为json字符串,以

nomethoderror undefined method 'map' for String:0x83....>
我所需要的只是其中的数据,这样我就可以得到将保存在用户对象上的每组json结果


我以前没有使用过json以及如何操作json,它在Jbuilder自述中解释了如何在模型中使用Jbuilder,但我一生都无法理解它。我曾试图尝试使用该示例,但它不起作用。

我跟踪了您的代码,也尝试了。我得到了此解决方案,因此您可以尝试此解决方案。确保定义正确的json对象

photos = [{"album"=>{"created_time"=>"123", "name"=>"hello"}}, {"album"=>{"created_time"=>"123", "name"=>"hello2"}}]

2.3.0 :011 > json.photos photos do |photo|
2.3.0 :012 >     json.created_time photo['album']['created_time']
2.3.0 :013?>   end

好吧,这让我有些迷惑,我想我尝试过这种语法,但我可能使用了一个符号,需要一个字符串表示。非常感谢!