简体   繁体   中英

Convert a Ruby Hash into JSON (without escape characters)

I have a Hash:

my_hash = {"bob.johnson@example.com"=>{"first"=>"Bob", "last"=>"Johnson"}, "lisa.dell@example.com"=>{"first"=>"Lisa", "last"=>"Dell"}}

When I try to serialize it with my_hash.to_json this is what I get:

"{\"bob.johnson@example.com\":{\"first\":\"Bob\",\"last\":\"Johnson\"},\"lisa.dell@example.com\":{\"first\":\"Lisa\",\"last\":\"Dell\"}}"

How could I convert a Hash to JSON format without getting the escaping characters?

These escape characters escape " in Ruby String (your my_hash.to_json output). If you do

puts my_hash.to_json

you'll see that actually these escape characters aren't added to output string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM