2016-04-06 14 views
0

不確定キー名とフィールド値の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 

答えて

0

json_element.map { |k,v| v['$'].replace_hash(hash = @replace_hash) }

私は上記で遊んとそれが動作するようになりました。

関連する問題