javascript - How to write proper algorithem serialize this special format of json data? -
i have csv file, looks this:
photosum,annee,mois
2732,2002,1
1030,2002,2
1661,2002,7
1450,2002,3
1308,2002,4
2131,2002,5
2891,2002,6
2748,2002,8
2140,2002,9
1499,2002,10
1211,2002,11
1398,2002,12
5376,2003,1
2780,2003,2
1927,2003,3
2266,2003,4
3670,2003,5
4071,2003,6
4764,2003,7
5261,2003,8
i want make csv file json data serialization this: the annual data monthly sort
{ "2002": [ 1, //january 22, //february 33, 44, 345, 45, 232, 43423, 324324, 324324, 324324, 6556 ], "2003": [ 3.5, 4, 4.3, 6.6, 11.7, 13.2, 16.2, 16.5, 12.7, 8.4, 5, 5.3 ] }
i trying use javascript convert csv json, write many kinds of regular expressions, failed, found have consider sequence of csv file carefully, complicated, can help me? please
function parsecsv(data, rowseperator) { var rows = data.split(rowseperator || '\n'); var json = {}; rows.slice(1).foreach(function (row) { var columns = row.split(','); var sum = parseint(columns[0]||0, 10) var year = parseint(columns[1], 10); var month = parseint(columns[2], 10); if (year && month) { json[year] = json[year] || []; json[year][month-1] = sum; } }); return json; } var json = parsecsv(csvdata);
Comments
Post a Comment