我有一个哈希:
my_hash = {"bob.johnson@example.com"=>{"first"=>"Bob", "last"=>"Johnson"}, "lisa.dell@example.com"=>{"first"=>"Lisa", "last"=>"Dell"}}
当我尝试用 my_hash.to_json
序列化它时,这是我得到的:
"{\"bob.johnson@example.com\":{\"first\":\"Bob\",\"last\":\"Johnson\"},\"lisa.dell@example.com\":{\"first\":\"Lisa\",\"last\":\"Dell\"}}"
如何在不获取转义字符的情况下将哈希转换为 JSON 格式?
最佳答案
这些转义字符转义 Ruby String
中的 "
(您的 my_hash.to_json
输出)。如果您这样做
puts my_hash.to_json
您会发现实际上这些转义字符并未添加到输出字符串中。
关于ruby-on-rails - 将 Ruby 哈希转换为 JSON(无转义字符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25523489/