【发布时间】:2015-07-23 21:05:50
【问题描述】:
我有一个 JSON 对象,用于 3 个日期的每个站点的流量。
我想在“日期”上将这三个对象合并在一起。下面的例子。
我正在使用 Ruby。最简单的方法是什么?
启动 JSON:
[
{
"Google":[
{
"Date":"2015-01-01",
"Value":100
},
{
"Date":"2015-02-01",
"Value":200
},
{
"Date":"2015-03-01",
"Value":300
}
]
},
{
"Yahoo":[
{
"Date":"2015-01-01",
"Value":1200
},
{
"Date":"2015-02-01",
"Value":1300
},
{
"Date":"2015-03-01",
"Value":1400
}
]
},
{
"Bing":[
{
"Date":"2015-01-01",
"Value":500
},
{
"Date":"2015-02-01",
"Value":600
},
{
"Date":"2015-03-01",
"Value":700
}
]
}
]
结束 JSON:
[
{
"Date":"2015-01-01",
"Google":100,
"Yahoo":1200,
"Bing":500
},
{
"Date":"2015-01-02",
"Google":200,
"Yahoo":1200,
"Bing":600
},
{
"Date":"2015-01-03",
"Google":300,
"Yahoo":1400,
"Bing":700
}
]
【问题讨论】:
-
我会在 mongo 中使用 map reduce,但如果你想在 Ruby 中使用,请考虑 ruby-doc.org/core-2.2.2/Enumerable.html#method-i-group_by 和 JSON parse/dump
-
我在问题中看不到任何 JSON 内容。
-
@undur_gongor 作者提到他有一个json数据。
-
@CodeGroover:但所有呈现的数据都只是普通的 Ruby 数据(数组、哈希、字符串),没有 JSON。
-
你说得对 :) - 数据以 JSON 开头,然后我刚刚做了
JSON.parse,所以是的,这个问题实际上只与哈希数组有关。 JSON 标签只是帮助那些希望合并相似 JSON 结构的人。