//DataTable'ı json olarak döndürmek isterseniz 2 basit yol var. Birisi JavaScriptSerializer diğeri ise Newtonsoft.Json.
// 1- JavaScriptSerializer
using System.Web.Script.Serialization;
public string DataTableToJsonWithJavaScriptSerializer(DataTable table)
{
var jsSerializer = new JavaScriptSerializer();
var parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataRow row in table.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return jsSerializer.Serialize(parentRow);
}
//2- JsonConvert
using Newtonsoft.Json;
public string DataTableToJsonWithJsonNet(DataTable table)
{
var jsonString=string.Empty;
jsonString = JsonConvert.SerializeObject(table);
return jsonString;
}