ruby - How to write to a JSON file in the correct format -


i creating hash in ruby , want write json file, in correct format.

here code:

temphash = {     "key_a" => "val_a",     "key_b" => "val_b" } fjson = file.open("public/temp.json","w") fjson.write(temphash) fjson.close 

and here contents of resulting file:

key_aval_akey_bval_b 

i'm using sinatra (don't know version) , ruby v 1.8.7.

how can write file in correct json format?

require json library, , use to_json.

require 'json' temphash = {     "key_a" => "val_a",     "key_b" => "val_b" } file.open("public/temp.json","w") |f|   f.write(temphash.to_json) end 

your temp.json file looks like:

{"key_a":"val_a","key_b":"val_b"} 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -