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
给出我想要的答案,但返回 nil
。
hash.to_json
用反斜杠返回我想要的答案。我不想要反斜杠。
最佳答案
这只是因为 String#inspect
。没有反斜杠。试试:
hjs = hash.to_json
puts hjs
https://stackoverflow.com/questions/4916563/