Rails4 JSON参数创建时解析错误

时间:2014-11-19 06:20:29

标签: ruby-on-rails json ruby-on-rails-3 ruby-on-rails-4

我正在使用RoR为Android构建API,我从Android获取用于创建对象的参数如下所示,

{"company"=>"{\"description\":\"Description of the company\",\"name\":\"Google\"}", "location"=>"Bangalore", "display_photo"=>#<ActionDispatch::Http::UploadedFile:0xb12dfa8 @tempfile=#<Tempfile:/tmp/RackMultipart20141119-6448-ewg4bk>, @original_filename="Male-Face-A1-icon.png", @content_type="image/*", @headers="Content-Disposition: form-data; name=\"display_photo\"; filename=\"Male-Face-A1-icon.png\"\r\nContent-Type: image/*\r\nContent-Length: 15460\r\nContent-Transfer-Encoding: binary\r\n">}

但是这行在解析时会出错

"company"=>"{\"description\":\"Description of the company\",\"name\":\"Google\"}"

因为,Rails应该是这样的,

"company"=>{"description":"Description of the company","name":"Google"}

如何在Rails中实现这一点......

请参阅Android请求Rails服务器的此链接, Android Code

1 个答案:

答案 0 :(得分:1)

在你的参数中:

"company"=>

是一个哈希键

"{\"description\":\"Description of the company\",\"name\":\"Google\"}"

是一个字符串,哈希值

[2] pry(main)> JSON.parse "{\"description\":\"Description of the company\",\"name\":\"Google\"}"
=> {"description"=>"Description of the company", "name"=>"Google"}

给出正确的输出