How to serialize Deserialize DataTable to json file

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
We can store data in xml file from data table and also we can load data from xml file into data table. how to serialize Deserialize DataTable to json file ?

i want to go for json because it size is smaller than xml.

public string DataTableToJSONWithJavaScriptSerializer(DataTable table)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List < Dictionary < string, object >> 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);
}

with help of JavaScriptSerializer we can store data in json format from data table but how could i read json data and store into datatable ?



please share the knowledge.

Continue reading...
 
Back
Top