Rails JSON解码和to_json不想一起工作?

时间:2012-02-08 11:36:55

标签: ruby-on-rails-3 json

> ActiveSupport::JSON.decode(13.to_json)
MultiJson::DecodeError: 743: unexpected token at '13'
    from /usr/lib/ruby/gems/1.8/gems/json-1.5.4/lib/json/common.rb:148:in `parse'
    from /usr/lib/ruby/gems/1.8/gems/json-1.5.4/lib/json/common.rb:148:in `parse'
    from /usr/lib/ruby/gems/1.8/gems/multi_json-1.0.4/lib/multi_json/engines/json_common.rb:9:in `decode'
    from /usr/lib/ruby/gems/1.8/gems/multi_json-1.0.4/lib/multi_json.rb:76:in `decode'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/json/decoding.rb:12:in `decode'

那么为什么?

1 个答案:

答案 0 :(得分:1)

这是因为JSON.decode期望比单个整数更复杂的结构。尝试包装它:

ActiveSupport::JSON.decode({:my_integer => 13}.to_json)

在我的观点中,我将此称为rails中的错误。