rails add to json response
I've done this before, but can't remember how its done now.
Have one json response from foursquare which includes:
"hereNow"=>{"count"=>1, "groups"=>[{"type"=>"friends", "name"=>"friends here", "count"=>0, "items"=>[]}, ...]}
I respond with @place (which is the location info) but want to pass the names and images to my view as json.
thought is was something like @place['hereNow'] << response['venu开发者_运维问答e']['hereNow']
to include it to my render :json => @place
Well the point is: the received json
is a string
so you can't work on it directly.
So two steps here:
convert the received
json
to aHash
easily add whatever you desire to the
Hash
convert the
hash
back tojson
There are great examples here.
In a nutshell:
j = ActiveSupport::JSON
hash = { :color => ["red", "green", "jellow"], :date => Time.now }
json_string = j.encode hash
recreated_hash = j.decode json_string
精彩评论