不確定キー名とフィールド値のJSONハッシュを、値に文字列またはサブストリングを代入したいと考えています。たとえば:rubyを使って要素をハッシュに置き換えるにはどうすればいいですか?
json_element.map { |k,v| v.map { |k1,v1| v1.replace_hash(hash={"hope" => "life" })} }
私のようにreplace_hash
を作成:
{"COLLECTION"=>{"$"=>"biblestudy"}, "JSON"=>{"$"=>"{ \"type\" : \"reaction\", \"verse_reaction\": \"That our lives should be one of giving life\", \"verse_key\" : \"109dc58b8020681e\" }"}, "KEY"=>{"$"=>"key.verse_reaction"}, "REPLACE-KEY"=>{"$"=>"replace-key"}}
私は次のことを試してみました{"replace-key" : "this is the replacement"}
を持っているに保存されているいくつかの値でreplace-key
を置き換えるためにしたいことメソッドString
:
def replace_hash(hash=nil)
if hash.present?
hash.each do | key, substitute |
self.sub! key, substitute
end
end