【发布时间】:2012-06-04 09:55:11
【问题描述】:
我正在使用 Rails 3.2 serialization 将 ruby 对象转换为 json。
例如,我已将 ruby 对象序列化为以下 json
{
"relationship":{
"type":"relationship",
"id":null,
"followed_id": null
}
}
在我的类关系中使用以下序列化方法 ActiveRecord::Base
def as_json(opts = {})
{
:type => 'relationship',
:id => id,
:followed_id => followed_id
}
end
我需要用空字符串替换空值,即空双引号,以响应 json。
我怎样才能做到这一点?
最好的问候,
【问题讨论】:
-
why do what to do that,如果它有nil,你会进一步看到什么问题?
-
Objective C 不能将 null 识别为像 true/false 这样的特殊字符串。
标签: ruby-on-rails ruby json serialization ruby-on-rails-3.2