【发布时间】:2015-12-03 04:15:09
【问题描述】:
我想去掉散列中每个属性内的 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
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 hash