我可以使用单行键
require 'json'
def saveUserLib(user_lib)
File.open("/Users/name/Documents/user_lib.json","w") do |f|
f.write($user_lib.to_json)
end
end
uname = gets.chomp
$user_lib["_uname"] = uname
saveUserLib($user_lib)
但是我怎样才能把它恢复为我的user_lib呢?
发布于 2012-01-30 01:26:57
下面是一些示例:
require 'json'
source_hash = {s: 12, f: 43}
json_string = JSON.generate source_hash
back_to_hash = JSON.parse json_string
发布于 2012-01-30 01:28:06
JSON.load
将会做到这一点。下面是一个双向的例子:
>> require 'json'
=> true
>> a = {"1" => "2"}
=> {"1"=>"2"}
>> b = JSON.dump(a)
=> "{\"1\":\"2\"}"
>> c = JSON.load(b)
=> {"1"=>"2"}
https://stackoverflow.com/questions/9055096
复制相似问题