C#实现DataTable,List,Json和对象集合以及json序列化转换的方法
DataTable转字符串
public static string DataTableToJsonWithJavaScriptSerializer(DataTable table)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataRow row in table.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return jsSerializer.Serialize(parentRow);
}
DataTable转json 时间格式化
public static string DataTableToJson(DataTable dt)
{
if (dt == null && dt.Rows.Count <= 0)
{
return "";
}
else
{
IsoDateTimeConverter convert = new IsoDateTimeConverter();
convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
return JsonConvert.SerializeObject(dt, Formatting.None, convert);
}
}
List转换成DataTable
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable dt = new DataTable();
for (int i = 0; i < properties.Count; i++)
{
PropertyDescriptor property = properties[i];
dt.Columns.Add(property.Name, property.PropertyType);
}
object[] values = new object[properties.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = properties[i].GetValue(item);
}
dt.Rows.Add(values);
}
return dt;
}
List转成json
public static string ObjectToJson<T>(string jsonName, IList<T> IL)
{
StringBuilder Json = new StringBuilder();
Json.Append("{\"" + jsonName + "\":[");
if (IL.Count > 0)
{
for (int i = 0; i < IL.Count; i++)
{
T obj = Activator.CreateInstance<T>();
Type type = obj.GetType();
PropertyInfo[] pis = type.GetProperties();
Json.Append("{");
for (int j = 0; j < pis.Length; j++)
{
Json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(IL[i], null) + "\"");
if (j < pis.Length - 1)
{
Json.Append(",");
}
}
Json.Append("}");
if (i < IL.Count - 1)
{
Json.Append(",");
}
}
}
Json.Append("]}");
return Json.ToString();
}
}
对象集合转json
public static string ToJson(IEnumerable array)
{
string jsonString = "[";
foreach (object item in array)
{
jsonString += Json.ToJson(item) + ",";
}
return Json.DeleteLast(jsonString) + "]";
}
dataset转换成json
public static string ToJson(DataSet dataSet)
{
string jsonString = "{";
foreach (DataTable table in dataSet.Tables)
{
jsonString += "\"" + ToJson(table.TableName) + "\":" + ToJson(table) + ",";
}
return jsonString = DeleteLast(jsonString) + "}";
}
json反序列化
public static T GetObjFromStr(string t)
{
return JsonConvert.DeserializeObject<T>(t);
}
json序列化
public static string GetStrFromObj(T t)
{
return JsonConvert.SerializeObject(t);
}
**object全部字段json化**
public static string ToJSON(object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
object全部字段json化,带count
public static string ToJSON(object obj, int rowCount)
{
string strJson = ToJSON(obj);
string strTemp = "totalCount:{0},data:[{1}]";
return "{" + String.Format(strTemp, rowCount, strJson) + "}";
}
List全部字段json化
public static string ToJSON(IList list, int rowCount)
{
string strJson = ToJSON(list);
string strTemp = "totalCount:{0},data:{1}";
return "{" + String.Format(strTemp, rowCount, strJson) + "}";
}