How to change the format of a dictionary?

advertisements

In a MVC JsonResult action I bind data to a dictionary and return it through JSON. It gives me data in a different form than what I require.

My code:

var query = obj.Where(x => x.Date > new DateTime(01 / 01 / 2000)
                        && x.Date <= Convert.ToDateTime(shortDate))
               .GroupBy(x => x.Date)
               .Select(x => new { LogDate = x.Key, Count = x.Count() });

Dictionary<string, int> openWith =  new Dictionary<string, int>();
foreach (var output in query)
{
   openWith.Add(output.LogDate.ToShortDateString(), output.Count);
}
string letter = "letter";
var chartsdata = openWith;
return Json(chartsdata,letter, JsonRequestBehavior.AllowGet);


The JSON data comes in the form:

var data1 = {
    "01/28/2015": 1,
    "01/30/2015": 6,
    "01/29/2015": 1,
    "01/22/2015": 3,
    "01/20/2015": 1,
    "01/10/2015": 5 }

To draw a graph I need the data in the form:

var data1 = [
    [gd(2015, 1, 28), 1],
    [gd(2015, 1, 30), 6],
    [gd(2015, 1, 29), 1],
    [gd(2015, 1, 22), 3],
    [gd(2015, 1, 20), 1],
    [gd(2015, 1, 10), 5] ]

Please let me know if anyone knows: how can I change the format of data?


Try this.

foreach (var output in query)
{
    openWith.Add("gd(" + output.LogDate.Year + ", " +  output.LogDate.Month + ", "
    + output.LogDate.Day + ")", output.Count);
}