如何将数据附加到 ruby​​/rails 中的 json?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3146980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:50:23  来源:igfitidea点击:

how to append data to json in ruby/rails?

ruby-on-railsrubyjson

提问by David

Say i have this short code:

说我有这个短代码:

item = Item.find(params[:id])
render :json => item.to_json

but i needed to insert/push extra information to the returned json object, how do i do that?

但我需要向返回的 json 对象插入/推送额外信息,我该怎么做?

Lets say i need to insert this extra info:

假设我需要插入这个额外的信息:

message : "it works"

Thanks.

谢谢。

回答by Dogbert

item = Item.find(params[:id])
item["message"] = "it works"
render :json => item.to_json

回答by NM.

The to_jsonmethod takes an option object as parameter. So what you can do is make a method in your item class called as message and have it return the text that you want as its value .

to_json方法将一个选项对象作为参数。因此,您可以做的是在您的项目类中创建一个名为 message 的方法,并让它返回您想要的文本作为其值。

class Item  < ActiveRecord::Base
 def message
  "it works"
 end
end

render :json => item.to_json(:methods => :message)

回答by Steve

I found the accepted answer now throws deprecation warnings in Rails 3.2.13.

我发现接受的答案现在会在 Rails 3.2.13 中引发弃用警告。

DEPRECATION WARNING: You're trying to create an attribute message'. Writing arbitrary attributes on a model is deprecated. Please just useattr_writer` etc.

弃用警告:您正在尝试创建属性message'. Writing arbitrary attributes on a model is deprecated. Please just useattr_writer` 等。

Assuming you don't want to put the suggested attr_writer in your model, you can use the as_jsonmethod (returns a Hash) to tweak your JSON response object.

假设您不想将建议的 attr_writer 放入模型中,您可以使用该as_json方法(返回一个哈希)来调整您的 JSON 响应对象。

item = Item.find(params[:id])
render :json => item.as_json.merge(:message => 'it works')

回答by Stanley Shauro

How to append data to json in ruby/rails 5

如何在 ruby​​/rails 5 中将数据附加到 json

If you use scaffold, e.g.:

如果您使用脚手架,例如:

rails generate scaffold MyItem

in the view folder you will see next files:

在视图文件夹中,您将看到下一个文件:

app/view/my_item/_my_item.json.jbuilder
app/view/my_item/index.json.jbuilder

so, you can add custom data to json output for an item, just add this:

因此,您可以将自定义数据添加到项目的 json 输出中,只需添加以下内容:

json.extract! my_item, :id, :some_filed, :created_at, :updated_at
json.url my_item_url(my_item, format: :json)    

json.my_data my_function(my_item)

As you can see, it's possible to modify as one item json output, as index json output.

如您所见,可以修改为一项 json 输出,作为索引 json 输出。

回答by Leandro Castro

I always use:

我总是使用:

@item = Item.find(params[:id])
render json: { item: @item.map { |p| { id: p.id, name: p.name } }, message: "it works"  } 

回答by Suprie

Have you tried this ?

你试过这个吗?

item = Item.find(params[:id]) 
item <<{ :status => "Success" }

render :json => item.to_json