1

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
            }
       }
1
  • I think theres a serializeJSON class or whatever available for C#/vb, may be worth looking into, its what i use for my ashx handlers to generate datatables json Commented May 29, 2013 at 2:50

2 Answers 2

3

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Thanks it got worked... thanks lot... i did the way u suggested but one change what i did is, if i call new it was throwing an error saying no best data type found for implicit array, so i just changed to new string[] {...}, it worked thank u so much..
1

This is an example of how to get your desired format:

for the ease of the exercise lets use a "Model" as the data object class:

public class Model{
    public string Property1{get;set;}
    public string Property2{get;set;}
    public string Property3{get;set;}
    public string Property4{get;set;}
}

Then you just need to go over it and create your desired json structure:

var ReportData = new List<Model>{
new Model{Property1 = "Trident", Property2 = "Internet Explorer 4.0", Property3 = "Win 95+", Property4 = "4"},
new Model{Property1 = "Gecko", Property2 = "Firefox 1.5", Property3 = "Win 98+ / OSX.2+", Property4 = "1.8"}
};

var aaData = ReportData.Select(l => new [] {l.Property1, l.Property2, l.Property3, l.Property4} ).ToArray();

5 Comments

I'm not getting select method on List object :(. anything we need to extend for list?
You need to return the JSON value in the same way as you did in your code: return Json( new {aaData = aaData})
The Select method is part of the LINQ infra, you should add "using System.Linq". But you don't have to use this LINQ syntax, you can also use a simple foreach loop instead.
could you pls post how to do using foreach, i'm not getting.. mean time i will try.. thanks for reply
thanks for the list code, i m trying.. will get back to u thanks very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.