我有一些结果:
puts result
看起来像这样的输出:
Allowed
20863963
1554906
Denied
3607325
0
Quarantined
156240
0
调试
p results
输出
[["Allowed", 20863963, 1554906], ["Denied", 3607325, 0], ["Quarantined", 156194, 0]]
标题是:
status,hits,page_views
我需要将其转换为 json。如果结果是标准 csv 格式,那么这将是直截了当的,但如果结果格式看起来像上面那样,如何处理呢?
预期输出与此类似:
[{"status":"Allowed","hits":"20863963","page_views":"1554906"},{"status":"Denied","hits":"3607325","page_views":"0"},{"status":"Quarantined","hits":"156240","page_views":"0"}]
解决方案
a = result.map{|s| {status: s[0], hits: s[1].to_i, page_views: s[2].to_i} }
puts a.to_json