Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails在JSON中序列化然后反序列化同一对象_Ruby On Rails_Json - Fatal编程技术网

Ruby on rails RubyonRails在JSON中序列化然后反序列化同一对象

Ruby on rails RubyonRails在JSON中序列化然后反序列化同一对象,ruby-on-rails,json,Ruby On Rails,Json,我花了几天时间来尝试所有JSON库,包括JSON gem、Yajl ruby、ActiveSupport::JSON,但没有任何东西可以完成这个简单的任务: user = User.find(1) json = user.to_json user2 = json.as_json # OR json.from_json # OR JSON.parse(json) # OR Yajl::Parser.parse(

我花了几天时间来尝试所有JSON库,包括JSON gem、Yajl ruby、ActiveSupport::JSON,但没有任何东西可以完成这个简单的任务:

user = User.find(1)
json = user.to_json
user2 = json.as_json # OR json.from_json 
                     # OR JSON.parse(json) 
                     # OR Yajl::Parser.parse(json)
                     # OR ActiveSupport::JSON.decode(json)
当我尝试使用反序列化的JSON数据创建新用户时,会出现以下错误之一:

User.new(Yajl::Parser.parse(json))  
# ActiveRecord::UnknownAttributeError: unknown attribute: json_class

User.new(json.as_json)
# NoMethodError: undefined method `stringify_keys!' for #<String:0xb6de4318>

User.new(JSON.parse(json))
# NoMethodError: undefined method `stringify_keys!' for #<User:0xb6dc7470>

User.new(ActiveSupport::JSON.decode(json))
# NoMethodError: undefined method `stringify_keys!' for #<User:0xb6da3764>
User.new(Yajl::Parser.parse(json))
#ActiveRecord::UnknownAttributeError:未知属性:json\u类
User.new(json.as_json)
#NoMethodError:未定义的方法“stringify\u keys!”为了#
User.new(JSON.parse(JSON))
#NoMethodError:未定义的方法“stringify\u keys!”为了#
User.new(ActiveSupport::JSON.decode(JSON))
#NoMethodError:未定义的方法“stringify\u keys!”为了#
这很容易用YAML实现,使用to_YAML和from_YAML。为什么to_json不能生成那种可以从_json转换回ruby对象的json

编辑:我正在使用Rails 2.3.8,尽管我已经尝试过2.3.5和3.0.0。此外,我正在使用json rpc插件创建json-rpc服务,尽管没有该插件,同样的问题仍然存在

json = Episode.first.to_json
dec = ActiveSupport::JSON.decode json
Episode.new dec['episode']
在Rails 3.0.1中为我工作。在您的示例中,在将JSON转换回Ruby之后,并没有从包装器dict中取出实际的属性dict。你应该试试

User.new(ActiveSupport::JSON.decode(json)['user'])
在Rails 3.0.1中为我工作。在您的示例中,在将JSON转换回Ruby之后,并没有从包装器dict中取出实际的属性dict。你应该试试

User.new(ActiveSupport::JSON.decode(json)['user'])

你确定你的json真的是json吗?就错误而言,它更像是用户对象:(你确定你的json真的是json吗?就错误而言,它更像是用户对象:(是的,这应该可以工作。另一个选项,只序列化属性:User.first.attributes.to_json。只序列化属性就可以了。
User.new(ActiveSupport::json.decode(json)['User']
没有。
json
存储为字符串,解码后是具有空属性的用户对象。
User.new(ActiveSupport::json.decode(json)['attributes'])
在Rails 3.yup中工作,这应该可以。另一个选项,仅序列化属性:User.first.attributes.to_json。仅序列化属性有效。
User.new(ActiveSupport::json.decode(json)[User']
没有。
json
存储为字符串,解码后它是一个具有空属性的用户对象。
User.new(ActiveSupport::json.decode(json)['attributes'])
在Rails 3中工作。