开发者

Ruby data structure to render a certain JSON format

[
   {
      "id":"123",
      "name":"House"
   },
   {
      "id":"1456",
      "name":"Desperate Housewives"
   },
   {
      "id":"789",
      "name":"Dollhouse"
   },
   {
      "id":"10",
      "name":"Full House"
   }
]

How can I render to produce this JSON format from within Ruby? I have all the data from the DB (@result) and don't know what data structure to use in Ruby that will render to this when I do this:

respond_t开发者_运维百科o do |format|    
    format.json { render :json => @result}
end

What data structure should @result be and how can I iterate to produce it? Thank you!


If @result is an array of ActiveRecord model instances then render :json => @result will produce something like what you are after, but will include all the attributes of the model (render calls to_json on the object you pass it unless it is a string).

To only include the id and name attributes, you can use the :only parameter of to_json:

respond_to do |format|    
  format.json { render :json => @result.to_json(:only => [:id, :name] }
end

Alternatively, you can create a array of Hash objects that only contain the required attributes:

respond_to do |format|    
  format.json { render :json => 
                  @result.collect {|o| {:id => o.id, :name => o.name} } }
end

Edit: See @dt's comment below. There is an attribute in the model named text that needs to be output as name. This can be done by creating an alias for text in the model:

class Model < ActiveRecord::Base
  alias_method :name, :text

and including the name using :methods:

respond_to do |format|    
  format.json { render :json => @result.to_json(:only => :id, :methods => :name }
end

Alternatively, the array of hashes approach can be used to rename the attribute:

respond_to do |format|    
  format.json { render :json => 
                  @result.collect {|o| {:id => o.id, :name => o.text} } }
end


Try using the json gem. It will allow to you do things like

@result.to_json

to convert your structures (say, a Hash) to json format. If you're using Ruby on Rails, this functionality should already be accessible to you, so just create a Hash and call to_json.


To create a JSON object of that particular format, you need a ruby array containing hashes for its elements.

@result = [{:id => "10", :name => "Full House"}, {:id => "789", :name => "blahblah"},...]

Rails will convert the ruby array to json correctly in your render response block:

respond_to do |format|    
  format.json { render :json => @result}
end


I was using a jQuery plugin (FCBKComplete) that needed the json results with specific key names, specifically 'caption' and 'value', which did not exist in the array I was calling to_json on.

So I did this (hacky, but it works):

render :json => taggings.map { |t| {:caption => t.tag.name, :value => t.tag.name} }.to_json

Where taggings is an array returned by an ActiveRecord find, and that returns json like so:

[{"value":"tag.a","caption":"tag.a"},{"value":"tag.b","caption":"tag.b"}]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜