Convert Generic List by Json for Datatables grid

advertisements

I am using Datatables grid in my MVC application to show the data. I'm not getting how to convert the Var which contains the generic list to JSON which in turn will be the input for Datatables.

Here is the JSON format for Datatables:

return Json(new
{
  aaData = new[] {
    new [] { "Trident", "Internet Explorer 4.0", "Win 95+", "4", "X" },
    new [] { "Gecko", "Firefox 1.5", "Win 98+ / OSX.2+", "1.8", "A" },
    new [] { "Webkit", "iPod Touch / iPhone", "iPod", "420.1", "A" },
  }
}, JsonRequestBehavior.AllowGet);

But the above one is hardcoded one, I'm getting generic list in runtime, how to iterate and change to JSON format like above.

Here is my code.

public void GetReport()
        {
            var ReportData = _homeService.GetReportDatafromDB();

            if (ReportData .Count > 0)
            {
               //Dont no how to convert for JSON data format here
            }
       }


Using the foreach syntax it will look something like this:

var aaData = new List<string[]>();
foreach(var l in ReportData){
    aaData.Add(new [] {l.Property1, l.Property2, l.Property3, l.Property4});
}

var thisIsYourResult = aaData.ToArray();