重命名返回的 JSON
我需要修改 Ruby 应用程序返回的 JSON。 从我的谷歌搜索来看,我似乎必须在我的类中重写 as_json ,但我无法让它工作
JSON:
"foo_class" = {
"created_at" = "2011-03-30T11:45:48Z";
#And so forth....
我想将 foo_class 修改为 foo ,即类名它自己,并且没有任何属性。
class FooClass < ActiveRecord::Base
def as_json(options={})
json = super(options)
#How do i modify only the class name in returned json?
#And leave the properties as is...
return json
end
I have the need for modifying JSON returned by a Ruby app.
From my googling it seems that I have to override as_json in my class, but I am not able to get it to work
JSON:
"foo_class" = {
"created_at" = "2011-03-30T11:45:48Z";
#And so forth....
I would like to modify foo_class to foo, i.e. the class name it self,and none of the properties.
class FooClass < ActiveRecord::Base
def as_json(options={})
json = super(options)
#How do i modify only the class name in returned json?
#And leave the properties as is...
return json
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Rails 3.0.5 中,您可以在控制器中的
as_json
方法中使用选项:root
:您可以使用 github 上的此应用,遵循路径
http://localhost:3000/foo_classes.js
In rails 3.0.5 you can use option
:root
inas_json
method in your controller:You can check it with this app on github, following path
http://localhost:3000/foo_classes.js