将Json解析的字符串还原为Json

时间:2016-04-08 06:21:01

标签: ruby-on-rails ruby json

我在Ruby on Rails上工作。我有一个像这样的Json字符串

"{\"user\":{\"email\":\"blah@blah.com\",\"name\":\"blah Test 1051\",\"mobile\":\"9999999999\",\"dob\":\"00/00/0000\",\"street\":\"somewhere\",\"state\":\"something\",\"city\":\"nowhere\",\"pincode\":\"50000\",\"Event_Code\":\"e0031\"}}"

我需要将此转换为JSON哈希,如此

{"user"=>{"email"=>"blah@blah.com", "name"=>"blah Test 1051", "mobile"=>"9999999999","dob"=>"00/00/0000","street"=>"somewhere","state"=>"something","city"=>"nowhere","pincode"=>"50000","Event_Code"=>"e0031"}}

2 个答案:

答案 0 :(得分:2)

require 'json'
h = JSON.parse string

此链接对您有所帮助..

https://hackhands.com/ruby-read-json-file-hash/

答案 1 :(得分:0)

JSON.parse "{\"user\":{\"email\":\"blah@blah.com\",\"name\":\"blah Test 1051\",\"mobile\":\"9999999999\",\"dob\":\"00/00/0000\",\"street\":\"somewhere\",\"state\":\"something\",\"city\":\"nowhere\",\"pincode\":\"50000\",\"Event_Code\":\"e0031\"}}"

检查http://apidock.com/ruby/JSON/parse以获取更多参考资料