DataTable
. DataTable
into a JSON object.
I have simple function to convert datatable to json string.
I have used Newtonsoft to generate string. I don't use Newtonsoft to totaly serialize Datatable. Be careful about this.
Maybe this can be useful.
private string DataTableToJson(DataTable dt) {
if (dt == null) {
return "[]";
};
if (dt.Rows.Count < 1) {
return "[]";
};
JArray array = new JArray();
foreach(DataRow dr in dt.Rows) {
JObject item = new JObject();
foreach(DataColumn col in dt.Columns) {
item.Add(col.ColumnName, dr[col.ColumnName]?.ToString());
}
array.Add(item);
}
return array.ToString(Newtonsoft.Json.Formatting.Indented);
}