1

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.

AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197

3 Answers3

5

You can use JSON.NET which automatically serializes DataTables (amongst many other types!) to JSON.

Satinder singh
  • 10,100
  • 16
  • 60
  • 102
Ilya O.
  • 1,500
  • 13
  • 19
  • 1
    You can essentially forget about anything that's built into the .NET Fx regarding JSON and you'll feel infinitely better about it. (JSON.NET works and is incredible.) – cfeduke Nov 11 '12 at 01:30
  • 1
    Indeed. So much pulled hair has been saved ever since I used JSON.NET :D – Ilya O. Nov 11 '12 at 02:09
0

Apply the AsEnumerable method on the data table and then apply some LINQ on that to get it in the form you want.

var thatList=(from p in dt.AsEnumerable() 
                 select new { 
                               Jan= p.Field<string>("Jan"),
                               Feb = p.Field<string>("Feb"),
                               Mar = p.Field<string>("Mar"),
                               Apr = p.Field<string>("Apr")
                            }).ToList();

Now you can use thatList to convert to Json.

return Json(new { status = true, chartData = thatList }, 
                                    JsonRequestBehavior.AllowGet);
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

Why don't you try something like this:

public static class JSONEncoderHelper
{
    public static string FromXML(DataTable table)
    {
        StringBuilder sbuilder = new StringBuilder();

        sbuilder.Append("{\"");
        sbuilder.Append(table.TableName);
        sbuilder.Append("\":[");

        bool first = true;
        foreach (DataRow drow in table.Rows)
        {
            if (first)
            {
                sbuilder.Append("{");
                first = false;
            }
            else
                sbuilder.Append(",{");

            bool firstColumn = true;
            foreach (DataColumn column in table.Columns)
            {
                if (firstColumn)
                {
                    sbuilder.Append(string.Format("\"{0}\":\"{1}\"", column.ColumnName, drow[column].ToString()));
                    firstColumn = false;
                }
                else
                sbuilder.Append(string.Format(",\"{0}\":\"{1}\"", column.ColumnName, drow[column].ToString()));
            }
            sbuilder.Append("}");
        }

        sbuilder.Append("]}");

        return sbuilder.ToString();
    }
}

All you have to do now is refactor that helper class I did in 2 minutes

:)

Leo L.
  • 1