2016-09-06 74 views
1

我有一些JSON數據存儲日期時間字符串和總數。例如將JSON作爲基於數據的新對象組合

[{ 
    "Date": "2012-04-01 12:00:00", 
    "Total": "14" 
}, { 
    "Date": "2012-04-02 06:00:00", 
    "Total": "3" 
}, { 
    "Date": "2012-04-02 14:00:00", 
    "Total": "12" 
}, { 
    "Date": "2012-04-02 16:00:00", 
    "Total": "5" 
}, { 
    "Date": "2012-04-02 17:00:00", 
    "Total": "7" 
}, { 
    "Date": "2012-04-03 06:00:00", 
    "Total": "9" 
}, { 
    "Date": "2012-04-03 14:00:00", 
    "Total": "2" 
}, { 
    "Date": "2012-04-04 06:00:00", 
    "Total": "1" 
}, { 
    "Date": "2012-04-04 14:00:00", 
    "Total": "10" 
}, { 
    "Date": "2012-04-04 19:00:00", 
    "Total": "8" 
}, { 
    "Date": "2012-04-04 21:00:00", 
    "Total": "4" 
}] 

我想要做的是創建新的JSON對象的數據是相同的年份,月份和日期。

因此,例如全套的數據可以被格式化爲:

DAY:

[{ 
    "Date": "2012-04-01" 
    "Total": "3" 
},{ 
    "Date": "2012-04-02" 
    "Total": "4" 
},{ 
    "Date": "2012-04-03" 
    "Total": "6" 
}] 

月:

[{ 
    "Date": "2012-04" 
    "Total": "36" 
},{ 
    "Date": "2012-05" 
    "Total": "11" 
},{ 
    "Date": "2012-06" 
    "Total": "23" 
},{ 
    "Date": "2012-07" 
    "Total": "17" 
}] 

年:

[{ 
    "Date": "2012" 
    "Total": "91" 
},{ 
    "Date": "2013" 
    "Total": "102" 
},{ 
    "Date": "2014" 
    "Total": "78" 
}] 

怎麼可能我這樣做是Rails? 如果我能得到一個我如何能做到其中一個的例子,那麼我可以用它作爲其他人的基礎!

所以在控制我有:

def get_count_day 
    render json: data.to_json 
end 

def get_count_month 
    render json: data.to_json 
end 

def get_count_year 
    render json: data.to_json 
end 

回答

1

嘿,你可以嘗試這樣的日

given_json.group_by{|b| b["Date"].to_date.strftime("%Y-%d-%m")}.collect{|key,value| {"Date" =>key , "Total" => value.sum{|d| d["Total"].to_i}}} 

單月

given_json.group_by{|b| b["Date"].to_date.strftime("%Y-%m")}.collect{|key,value| {"Date" =>key , "Total" => value.sum{|d| d["Total"].to_i}}} 

對於

given_json.group_by{|b| b["Date"].to_date.strftime("%Y")}.collect{|key,value| {"Date" =>key , "Total" => value.sum{|d| d["Total"].to_i}}} 
0

這裏是獲取他們白天分組的例子:

grouped_json = Hash.new(0) 
your_json_array.each do |array_element| 
    date = Date.parse(array_element['Date']).strftime('%Y-%m-%d') 
    grouped_json[date] += array_element['Total'] 
end 
grouped_json.map {|k,v| {'Date' => k, 'Total' => v} } 

通過一個月?

grouped_json = Hash.new(0) 
your_json_array.each do |array_element| 
    date = Date.parse(array_element['Date']).strftime('%Y-%m') 
    grouped_json[date] += array_element['Total'] 
end 
grouped_json.map {|k,v| {'Date' => k, 'Total' => v} } 

年份?

grouped_json = Hash.new(0) 
your_json_array.each do |array_element| 
    date = Date.parse(array_element['Date']).strftime('%Y') 
    grouped_json[date] += array_element['Total'] 
end 
grouped_json.map {|k,v| {'Date' => k, 'Total' => v} } 
2
count_by_day = 
    json.group_by{ |record| record[:Date].to_date.strftime } 
     .map { |k, v| { Date: k, Total: v.map { |y| y[:Total].to_i }.sum.to_s } } 

#=> [{:Date=>"2012-04-01", :Total=>"14"}, {:Date=>"2012-04-02", :Total=>"27"}, {:Date=>"2012-04-03", :Total=>"11"}, {:Date=>"2012-04-04", :Total=>"23"}] 

count_by_month = 
    json.group_by{ |record| record[:Date].to_date.strftime('%Y-%m') } 
     .map { |k, v| { Date: k, Total: v.map { |y| y[:Total].to_i }.sum.to_s } } 

#=> [{:Date=>"2012-04", :Total=>"75"}] 

count_by_year = 
    json.group_by{ |record| record[:Date].to_date.strftime('%Y') } 
     .map { |k, v| { Date: k, Total: v.map { |y| y[:Total].to_i }.sum.to_s } } 

#=> [{:Date=>"2012", :Total=>"75"}] 
+0

我得到一個錯誤'未定義的方法GROUP_BY ' – Cameron

+0

'json'這裏是你在問題 –

+0

中發佈的哈希數組,如果數據是'data.group_by ....' 04-01 12:00:00「, 」Total「:」14「 },{ 」Date「:」2012-04-02 06:00:00「, 」Total「:」3「 } .....]' –