2011-07-09 80 views
0

我已经完成了此操作,但不记得它现在如何完成。rails添加到json响应

有来自四方的一个JSON响应,其中包括:

"hereNow"=>{"count"=>1, "groups"=>[{"type"=>"friends", "name"=>"friends here", "count"=>0, "items"=>[]}, ...]} 

我@place回应(这是位置信息),但想的名字和图像传递给我的JSON视图。

思想是是像@place['hereNow'] << response['venue']['hereNow']将之列入我render :json => @place

+0

分配给它之前是什么@place [ '此时此地']?你想要什么? –

回答

1

那么的一点是:接收jsonstring,所以你不能直接使用。

所以两个步骤在这里:

  • 将接收jsonHash

  • 轻松地添加任何你想要的Hash

  • 转换hashjson

有很多例子here

一言以蔽之:

j = ActiveSupport::JSON 
hash = { :color => ["red", "green", "jellow"], :date => Time.now } 
json_string = j.encode hash 
recreated_hash = j.decode json_string  
+0

对我来说,它就像'hash = {:hereNow => response ['venue'] ['hereNow']}''一样简单。谢谢。 – pcasa