我想擺脫散列中每個屬性中的value: <value>
關鍵值。並使其如下所示:"total_interactions": 493.667
下面是不正確的格式,我希望在json中實現預期的良好格式。散列中的哈希修改問題,替換紅寶石中的值
{
"3": {
"total_interactions": {
"value": 493.667
},
"shares": {
"value": 334
},
"comments": {
"value": 0
},
"likes": {
"value": 159.66666666666666
},
"total_documents": 6
},
"4": {
"total_interactions": {
"value": 701
},
"shares": {
"value": 300
},
"comments": {
"value": 0
},
"likes": {
"value": 401
},
"total_documents": 1
}
}
我希望它是這樣的:
{
"3": {
"total_interactions": 493.6666666666667,
"shares": 334,
"comments": 0,
"likes": 159.66666666666666,
"total_documents": 6
},
"4": {
"total_interactions": 701,
"shares": 300,
"comments": 0,
"likes": 401,
"total_documents": 1
}
}
這裏是應該這樣做,但不工作的代碼。沒有影響。不知道什麼是錯的
# the result_hash variable is the first hash with value: <value>
result_hash.each do |hash_item|
hash_item.each do |key,value_hash|
if(!value_hash.nil?)
value_hash.each do |k,v|
hash_item[key] = v
end
end
end
end
什麼'哈希'在最後?你的意思是你正在迭代的散列是我的散列'result_hash'?對不起,我是ruby的新手 –
爲了解決你的問題中的散列問題,我將它設置爲變量'hash'。然後我在循環結束時再次輸出它,以表明它確實已經改變。我會編輯我的答案,使其更加明顯。 –
謝謝你的作品,我理解它!謝謝! –