我有一个json文件。我用它来存储信息,因此它不断地被读取和写入。
我对ruby和oop一般都很陌生,所以我确信我会以疯狂的方式解决这个问题。
class Load
def initialize(save_name)
puts "loading " + save_name
@data = JSON.parse(IO.read( $user_library + save_name ))
@subject = @data["subject"]
@id = @data["id"]
@save_name = @data["save_name"]
@listA = @data["listA"] # is an array containing dictionaries
@listB = @data["listB"] # is an array containing dictionaries
end
attr_reader :data, :subject, :id, :save_name, :listA, :listB
end
example = Load.new("test.json")
puts example.id
=> 937489327389749
所以我现在可以轻松读取json文件,但是如何写回文件 - 参考示例?说我想更改ID example.id.change(7129371289)
...或者将词典添加到列表A和B ......这可能吗?
-------------更新--------------
我有它的工作,但我相信它会冒犯别人......所以我把它包括在下面。
class Load
def initialize(save_name)
puts "debug printed from inside 'Load'"
puts "loading " + save_name
@data = JSON.parse(IO.read( $user_library + save_name ))
@subject = @data["subject"]
@id = @data["id"]
@is_a = @data["is_a"]
@save_name = @data["save_name"]
@listA = @data["listA"]
@listB = @data["listB"]
def append(dataID, input)
puts "debug printed from inside 'Load' 'append'"
puts dataID
puts input
if dataID == "ListA"
puts "yes this is ListA"
append = @data
append["listA"] << input
puts append
File.open( $user_library + append["save_name"], "w" ) do |f|
f.write(append.to_json)
end
end
if dataID == "ListB"
puts "yes this is ListB"
append = @data
append["listB"] << input
puts append
File.open( $user_library + append["save_name"], "w" ) do |f|
f.write(append.to_json)
end
end
end
end
attr_reader :data, :subject, :id, :save_name, :listA, :listB
end
puts "OPENING SAVED SUBJECT"
puts Load.new("animals.json").listA
puts "CALLING APPEND"
new_hash = {"cow" => "horse"}
Load.new("animals.json").append("ListA", new_hash )
答案 0 :(得分:6)
往返JSON的最简单方法是使用JSON
库来适当地转换数据:
json = my_object.to_json
- 关于创建JSON字符串的特定对象的方法。json = JSON.generate(my_object)
- 从对象创建JSON字符串。JSON.dump(my_object, someIO)
- 创建一个JSON字符串并写入文件。my_object = JSON.parse(json)
- 从JSON字符串创建Ruby对象。my_object = JSON.load(someIO)
- 从文件创建一个Ruby对象。取自this answer to another of your questions。
但是,如果您愿意,可以将其包装在一个类中:
class JSONHash
require 'json'
def self.from(file)
self.new.load(file)
end
def initialize(h={})
@h=h
end
# Save this to disk, optionally specifying a new location
def save(file=nil)
@file = file if file
File.open(@file,'w'){ |f| JSON.dump(@h, f) }
self
end
# Discard all changes to the hash and replace with the information on disk
def reload(file=nil)
@file = file if file
@h = JSON.parse(IO.read(@file))
self
end
# Let our internal hash handle most methods, returning what it likes
def method_missing(*a,&b)
@h.send(*a,&b)
end
# But these methods normally return a Hash, so we re-wrap them in our class
%w[ invert merge select ].each do |m|
class_eval <<-ENDMETHOD
def #{m}(*a,&b)
self.class.new @h.send(#{m.inspect},*a,&b)
end
ENDMETHOD
end
def to_json
@h.to_json
end
end
上面的行为就像一个哈希,但您可以使用foo = JSONHash.from("foo.json")
从磁盘加载,像往常一样修改该哈希,然后只需要foo.save
保存到磁盘。
或者,如果您在磁盘上没有文件开头:
foo = JSONHash.new a:42, b:17, c:"whatever initial values you want"
foo.save 'foo.json'
# keep modifying foo
foo[:bar] = 52
f.save # saves to the last saved location