ruby-on-rails - 将 Ruby 哈希转换为 JSON(无转义字符)

标签 ruby-on-rails ruby json serialization hash

我有一个哈希:

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/

相关文章:

ruby-on-rails - 运行 heroku rake db :migrate 时出现 Heroku 错误

ruby-on-rails - 提交 rails 2.3

ruby-on-rails - 如何重置 factory_girl 序列?

ruby-on-rails - 语法检查或 "Compiling"Ruby on Rails 应用程序

java - 删除android中json中的重复字段

javascript - 为什么我的 for 循环卡住了?

ruby-on-rails - RSpec 和 protected 方法,current_user 的 Controller 规范

ruby - 在 ruby​​ 中的哈希数组中查找包含键的哈希的最佳方法?

Ruby 修改原始变量

python - Pickle 拒绝使用 celery 报告 ContentDisallowed : Refusing to deserialize untrusted content of type pickle 来序列化内容