RubyでネストされたJSON文字列内のキーのすべての出現を削除しようとしていますが、JSONの構造は予測できません。例えば、ネストされたJSON内の任意の場所でキーを削除
{
"id": null,
"created-at": null,
"updated-at": null,
"locale": null,
"name": null,
"description": null,
"item": {
"id": null,
"created-at": null,
"updated-at": null,
"description": null,
"item-number": null,
"name": null
},
"uom": {
"id": null,
"created-at": null,
"updated-at": null,
"code": null,
"name": null
}
}
と私は彼らが置かれているところはどこでも、「更新-AT」の出現をすべて削除します。
私はこれを試しましたが、うまくいかないようです。私は間違って何をしているポインタ誰も与えることができますか?
def strip_keys(json, targetkey)
json.each_with_object([]) do |record, results|
record.each do |key, value|
if value.is_a? Hash
results << { key => strip_keys(value, targetkey) }
else
results << { key => value } unless key == targetkey
end
end
end
end
を質問タグの1つが示唆している)、[Hash#except](http://api.rubyonrails.org/classes/Hash.html#method-except)を使用する方法があるかもしれないが、私はそれを使用していない私の前に – Pend
これは素晴らしく、感謝しています!関数を呼び出すときに引数を反転させてしまったのですが(recursively_remove_property!(json、 "updated-at") – p1k4blu
ああ、固定されています)、私はそれをテストしていたメソッドを変更しましたが、私はここにそれを貼り付けた。それはあなたのために働いてうれしい – Pend