My Technical Notes

Tuesday, 19 May 2015

DataRow to JSON

Following `DataRowToJson` method converts a `DataRow` object into its JSON representation (`string`):


public static string DataRowToJson(DataRow datarow)
{
    var dict = new Dictionary<string, object>();
    foreach (DataColumn col in datarow.Table.Columns)
    {
        dict.Add(col.ColumnName, datarow[col]);
    }

    var jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

    return jsSerializer.Serialize(dict);
}

No comments: