C# DataTable to Json?

Posted by AliRiza Adiyahsi on Stack Overflow See other posts from Stack Overflow or by AliRiza Adiyahsi
Published on 2012-11-10T22:42:43Z Indexed on 2012/11/10 23:00 UTC
Read the original article Hit count: 168

Filed under:
|
|

I want to get DataTable as Json Format to show it on a chart.

public JsonResult GetDataTable()
{
    DataTable dt = new DataTable();

    dt.Columns.Add("Jan");
    dt.Columns.Add("Feb");
    dt.Columns.Add("Mar");
    dt.Columns.Add("Apr");

    for (int i = 0; i < 10; i++)
    {
        dt.Rows.Add(i * 5, i * 10, i * 15, i * 11);
    }

    // JsonDataTable = dt to Json

    return new JsonResult
    {
        Data = new
        {
            success = true,
                chartData = JsonDataTable 
        },
        JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };
}

How Can I convert DataTable to Json?

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about JSON