3

我有一个语言模型,我想将所有语言作为 json,但 json 输出如下所示

[{"language":{"created_at":null,"id":1,"language":"English","updated_at":null}},{"language":{"created_at":null,"id" :2,"language":"Swedish","updated_at":null}},{"language":{"created_at":null,"id":3,"language":"German","updated_at":null }},{"language":{"created_at":null,"id":4,"language":"French","updated_at":null}},{"language":{"created_at":null," id":5,"language":"spanish","updated_at":null}},{"language":{"created_at":null,"id":6,"language":"dutch","updated_at" :null}},{"语言":{"created_at":"2012-12-03T05:01:18Z","id":7,"language":"泰米尔语","updated_at":"2012-12-03T05:01:18Z"}}]

但我想把它变成

{"language":[{"created_at":null,"id":1,"language":"English","updated_at":null},{"created_at":null,"id":2,"language" :"瑞典语","updated_at":null},{"created_at":null,"id":3,"language":"德语","updated_at":null},{"created_at":null,"id" :4,"language":"French","updated_at":null},{"created_at":null,"id":5,"language":"spanish","updated_at":null},{"created_at" :null,"id":6,"language":"dutch","updated_at":null},{"created_at":null,"id":7,"language":"Tamil","updated_at":null } ] }

更新

def index
 @languages = Language.all
 respond_to do |format|
   format.json { render json: @languages}
 end
end

更新 2

    class Language < ActiveRecord::Base
        ActiveRecord::Base.include_root_in_json = false
        has_and_belongs_to_many :users 
    end
4

5 回答 5

15

我相信这应该有效:

format.json { render json: { "language" => @languages.as_json(:root => false) }.to_json }

@languages数组转换为没有根键的 JSON 格式的哈希模型数组(使用as_json),然后将结果包装在带有根键“语言”的哈希中,并将该哈希转换为 JSON 格式的字符串与to_json. (有关使用包含或不包含根节点的详细信息,请参阅文档as_json。)

例如,使用模型Post

posts = Post.all
#=> [#<Post id: 1, name: "foo", title: "jkl", content: "some content", created_at: "2012-11-22 01:05:46", updated_at: "2012-11-22 01:05:46">]

# convert to array of hashes with no root keys
posts.as_json(root: false)
#=> [{"content"=>"some content", "created_at"=>Thu, 22 Nov 2012 01:05:46 UTC +00:00, "id"=>1, "name"=>"foo", "title"=>"jkl", "updated_at"=>Thu, 22 Nov 2012 01:05:46 UTC +00:00}]

# add root back to collection:
{ "post" => posts.as_json(root: false) }
#=> {"post"=>[{"content"=>"some content", "created_at"=>Thu, 22 Nov 2012 01:05:46 UTC +00:00, "id"=>1, "name"=>"foo", "title"=>"jkl", "updated_at"=>Mon, 03 Dec 2012 09:41:42 UTC +00:00}]}

# convert to JSON-formatted string
{ "post" => posts.as_json(root: false) }.to_json    
#=> "{\"post\":[{\"content\":\"some content\",\"created_at\":\"2012-11-22T01:05:46Z\",\"id\":1,\"name\":\"foo\",\"title\":\"jkl\",\"updated_at\":\"2012-12-03T09:43:37Z\"}]}"
于 2012-12-03T06:31:34.663 回答
3

覆盖您要自定义的模型上的 as_json

def as_json options={}
    {
      id: id,
      login: login, 
      name: custom.value, #for custom name
     ...
    }
end

==> or

def as_json(options={})
  super(:only => [:id, :login, :name ....])
end

来自:这里

其他链接:这里

于 2016-04-04T01:12:55.540 回答
2

我建议您使用 rabl gem ( https://github.com/nesquena/rabl ) 来格式化您的数据。

于 2012-12-03T06:21:12.077 回答
0

在渲染 json 时添加自定义 json 输出的最简单方法是使用提供许多 json 模板的 gem -

https://github.com/fabrik42/acts_as_api

于 2012-12-03T07:17:23.670 回答
0

覆盖as_json模型中的方法,包括关联、隐藏列,为什么不呢?调用自定义方法,因为它们是属性

  def as_json(options={})
    super(:except => [:created_at,:updated_at],
          :include => { 
            :members => {
                :only => [:role, :account], 
                :include => {
                  :account => {
                    :only => [:name, :subdomain]
                  }
                }
              }
            },
          :methods => [:jwt_token]
    )
  end

这将输出如下内容:

{
  "id": 2,
  "name": "Test Teacher",
  "email": "teacher@testing.io",
  "jwt_token":"eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MiwiZXhwIjoxNTY2NzQ0OTQzfQ.HDGu7JiJEQEEpGo7inuXtOZBVQOfTaFquy8dr-QH5jY",

  "members": [{
        "role": "instructor",
        "account": {
            "name": "Testing",
            "subdomain": "test"
         }
  }],
}
于 2019-06-26T14:57:30.270 回答