Please check below link
you can use nuget package
newtonsoft json[
^]
and easily convert Any Collection or object to json
as generic collection , data set , data table,
make data table from xml and convert that to json .
JSON[
^]
ok
Then you can use
Here dt is DataTable
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
Use This Method
private StringBuilder DatasetToJson(DataSet ds)
{
var sb = new StringBuilder();
var serializer =
new JavaScriptSerializer();
var rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
row = dt.Columns.Cast<DataColumn>().ToDictionary(col => col.ColumnName, col => dr[col]);
rows.Add(row);
}
sb.Append(serializer.Serialize(rows));
}
return sb;
}