How do I convert a Hash to a JSON string in Ruby 1.9? -


ruby-1.9.2-p0 > require 'json'  => true  ruby-1.9.2-p0 > hash = {hi: "sup", yo: "hey"}  => {:hi=>"sup", :yo=>"hey"}  ruby-1.9.2-p0 > hash.to_json  => "{\"hi\":\"sup\",\"yo\":\"hey\"}" ruby-1.9.2-p0 > j hash {"hi":"sup","yo":"hey"}  => nil  

j hash puts answer want returns nil.

hash.to_json returns answer want backslashes. don't want backslashes.

that's because of string#inspect. there no backslashes. try:

hjs = hash.to_json puts hjs 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -