Hello everyone, I going to share the code sample
for convert Data Table into JSON string using JavaScript Serializer as given
below.
public class Converter
{
/// <summary>
/// This method is used
to convert DataTable into JSON string using JavaScript Serializer.
/// </summary>
public String
ConvertToJSONString(DataTable
dataTable)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<String, Object>> tableRows =
new List<Dictionary<String, Object>>();
Dictionary<String, Object> row;
foreach (DataRow dr in dataTable.Rows)
{
row = new Dictionary<String, Object>();
foreach (DataColumn col in dataTable.Columns)
{
row.Add(col.ColumnName,
dr[col]);
}
tableRows.Add(row);
}
return serializer.Serialize(tableRows);
}
}