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
。如何在Ruby 1.9中將哈希轉換爲JSON字符串?
hash.to_json
用反斜槓返回我想要的答案。我不想反斜槓。
什麼反斜槓? – Phrogz 2011-02-06 22:25:34