开发者

rails add to json response

开发者 https://www.devze.com 2023-03-19 13:58 出处:网络
I\'ve done this before, but can\'t remember how its done now. Have one json response from foursquare which includes:

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 a Hash

  • easily add whatever you desire to the Hash

  • convert the hash back to json

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    
0

精彩评论

暂无评论...
验证码 换一张
取 消