DataTable
. DataTable
into a JSON object. To access the convert datatable value in Json method follow the below steps:
$.ajax({
type: "POST",
url: "/Services.asmx/YourMethodName",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var parsed = $.parseJSON(data.d);
$.each(parsed, function (i, jsondata) {
$("#dividtodisplay").append("Title: " + jsondata.title + "<br/>" + "Latitude: " + jsondata.lat);
});
},
error: function (XHR, errStatus, errorThrown) {
var err = JSON.parse(XHR.responseText);
errorMessage = err.Message;
alert(errorMessage);
}
});
All of these answers are really great for moving data! Where they fail is preserving the column type of data being moved. This becomes a problem when you want to do things like merge datatables that appear to be the same. JsonConvert
will look at the first row of data to determine the column datatype, which may be guessed incorrectly.
To get around this;
DataTable
and DataColumn
definitions in separate response objects.DataColumn
definitions in the response before reading in the table.DataTable
ignoring the schema defined by Json.It sounds like a lot, but its only three additional lines of code.
// Get our Column definitions and serialize them using an anoymous function.
var columns = dt.Columns.Cast<DataColumn>().Select(c => new { DataPropertyName = c.ColumnName, DataPropertyType = c.DataType.ToString()});
resp.ObjSchema = JsonConvert.SerializeObject(columns);
resp.Obj = JsonConvert.SerializeObject(dt);
resp.ObjSchema
becomes;
[
{
"DataPropertyName": "RowId",
"DataPropertyType ": "System.Int32"
},
{
"DataPropertyName": "ItemName",
"DataPropertyType ": "System.String"
}
]
Instead of letting Json define the column definitions via dt = JsonConvert.DeserializeObject<DataTable>(response)
we can use LINQ on our resp.ObjSchema
to define them ourselves. We'll use MissingSchemaAction.Ignore
to ignore the schema provided by Json.
// If your environment does not support dynamic you'll need to create a class for with DataPropertyName and DataPropertyType.
JsonConvert.DeserializeObject<List<dynamic>>(response.ObjSchema).ForEach(prop =>
{
dt.Columns.Add(new DataColumn() { ColumnName = prop.DataPropertyName, DataType = Type.GetType(prop.DataPropertyType.ToString()) });
});
// Merge the results ignoring the JSON schema.
dt.Merge(JsonConvert.DeserializeObject<DataTable>(response.Obj), true, MissingSchemaAction.Ignore);
Very Simple these days..
string json = JsonConvert.SerializeObject(YourDataTable, Formatting.Indented);
Now Convert your Json to a DataTable:
YourDataTable = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
Works for DataSets as well..
Convert datatable to JSON using C#.net
public static object DataTableToJSON(DataTable table)
{
var list = new List<Dictionary<string, object>>();
foreach (DataRow row in table.Rows)
{
var dict = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
dict[col.ColumnName] = (Convert.ToString(row[col]));
}
list.Add(dict);
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(list);
}
An alternative way without using javascript serializer:
public static string DataTableToJSON(DataTable Dt)
{
string[] StrDc = new string[Dt.Columns.Count];
string HeadStr = string.Empty;
for (int i = 0; i < Dt.Columns.Count; i++)
{
StrDc[i] = Dt.Columns[i].Caption;
HeadStr += "\"" + StrDc[i] + "\":\"" + StrDc[i] + i.ToString() + "¾" + "\",";
}
HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);
StringBuilder Sb = new StringBuilder();
Sb.Append("[");
for (int i = 0; i < Dt.Rows.Count; i++)
{
string TempStr = HeadStr;
for (int j = 0; j < Dt.Columns.Count; j++)
{
TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + "¾", Dt.Rows[i][j].ToString().Trim());
}
//Sb.AppendFormat("{{{0}}},",TempStr);
Sb.Append("{"+TempStr + "},");
}
Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
if(Sb.ToString().Length>0)
Sb.Append("]");
return StripControlChars(Sb.ToString());
}
//To strip control characters:
//A character that does not represent a printable character but //serves to initiate a particular action.
public static string StripControlChars(string s)
{
return Regex.Replace(s, @"[^\x20-\x7F]", "");
}
Try this custom function.
public static string DataTableToJsonObj(DataTable dt)
{
DataSet ds = new DataSet();
ds.Merge(dt);
StringBuilder jsonString = new StringBuilder();
if (ds.Tables[0].Rows.Count > 0)
{
jsonString.Append("[");
for (int rows = 0; rows < ds.Tables[0].Rows.Count; rows++)
{
jsonString.Append("{");
for (int cols = 0; cols < ds.Tables[0].Columns.Count; cols++)
{
jsonString.Append(@"""" + ds.Tables[0].Columns[cols].ColumnName + @""":");
/*
//IF NOT LAST PROPERTY
if (cols < ds.Tables[0].Columns.Count - 1)
{
GenerateJsonProperty(ds, rows, cols, jsonString);
}
//IF LAST PROPERTY
else if (cols == ds.Tables[0].Columns.Count - 1)
{
GenerateJsonProperty(ds, rows, cols, jsonString, true);
}
*/
var b = (cols < ds.Tables[0].Columns.Count - 1)
? GenerateJsonProperty(ds, rows, cols, jsonString)
: (cols != ds.Tables[0].Columns.Count - 1)
|| GenerateJsonProperty(ds, rows, cols, jsonString, true);
}
jsonString.Append(rows == ds.Tables[0].Rows.Count - 1 ? "}" : "},");
}
jsonString.Append("]");
return jsonString.ToString();
}
return null;
}
private static bool GenerateJsonProperty(DataSet ds, int rows, int cols, StringBuilder jsonString, bool isLast = false)
{
// IF LAST PROPERTY THEN REMOVE 'COMMA' IF NOT LAST PROPERTY THEN ADD 'COMMA'
string addComma = isLast ? "" : ",";
if (ds.Tables[0].Rows[rows][cols] == DBNull.Value)
{
jsonString.Append(" null " + addComma);
}
else if (ds.Tables[0].Columns[cols].DataType == typeof(DateTime))
{
jsonString.Append(@"""" + (((DateTime)ds.Tables[0].Rows[rows][cols]).ToString("yyyy-MM-dd HH':'mm':'ss")) + @"""" + addComma);
}
else if (ds.Tables[0].Columns[cols].DataType == typeof(string))
{
jsonString.Append(@"""" + (ds.Tables[0].Rows[rows][cols]) + @"""" + addComma);
}
else if (ds.Tables[0].Columns[cols].DataType == typeof(bool))
{
jsonString.Append(Convert.ToBoolean(ds.Tables[0].Rows[rows][cols]) ? "true" : "fasle");
}
else
{
jsonString.Append(ds.Tables[0].Rows[rows][cols] + addComma);
}
return true;
}