DatatableToJson JsonToDatatable

时间:2022-09-27 21:38:14

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

/// <summary>
/// 将DataTable类型转为JSON类型供使用
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public string DataTableToJson(DataTable dt)
{
string result = "";
result = JsonConvert.SerializeObject(dt, new DataTableConverter());
return result;
}
/// <summary>
/// 将JSON类型转为DataTable类型供使用
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public DataTable JsonToDataTable(string json)
{
DataTable dt = JsonConvert.DeserializeObject<DataTable>(json);
return dt;
}