C#完成DataTable,List和Json轉換的辦法。本站提示廣大學習愛好者:(C#完成DataTable,List和Json轉換的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成DataTable,List和Json轉換的辦法正文
本文實例講述了C#完成DataTable,List和Json轉換的辦法。分享給年夜家供年夜家參考,詳細以下:
1. 將DataTable或Ilist<>轉換成JSON格局
using System; using System.Data; using System.Text; using System.Collections.Generic; using System.Reflection; /// <summary> /// 將DataTable或Ilist<>轉換成JSON格局 /// </summary> public class ToJson { public ToJson() { } //DataTable轉成Json public static string DataTableToJson(string jsonName, DataTable dt) { StringBuilder Json = new StringBuilder(); Json.Append("{\"" + jsonName + "\":["); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { Json.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\""); if (j < dt.Columns.Count - 1) { Json.Append(","); } } Json.Append("}"); if (i < dt.Rows.Count - 1) { Json.Append(","); } } } Json.Append("]}"); return Json.ToString(); } //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(); } }
2. 各類數據集轉換為json
/// <summary> /// 對象轉換為Json字符串 /// </summary> /// <param name="jsonObject">對象</param> /// <returns>Json字符串</returns> public static string ToJson(object jsonObject) { string jsonString = "{"; PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties(); for (int i = 0; i < propertyInfo.Length; i++) { object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null); string value = string.Empty; if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan) { value = "'" + objectValue.ToString() + "'"; } else if(objectValue is string) { value = "'" + ToJson(objectValue.ToString()) + "'"; } else if (objectValue is IEnumerable) { value = ToJson((IEnumerable)objectValue); } else { value = ToJson(objectValue.ToString()); } jsonString += "\"" + ToJson(propertyInfo[i].Name) + "\":" + value + ","; } return Json.DeleteLast(jsonString) + "}"; } /// <summary> /// 對象聚集轉換Json /// </summary> /// <param name="array">聚集對象</param> /// <returns>Json字符串</returns> public static string ToJson(IEnumerable array) { string jsonString = "["; foreach (object item in array) { jsonString += Json.ToJson(item) + ","; } return Json.DeleteLast(jsonString) + "]"; } /// <summary> /// 通俗聚集轉換Json /// </summary> /// <param name="array">聚集對象</param> /// <returns>Json字符串</returns> public static string ToArrayString(IEnumerable array) { string jsonString = "["; foreach (object item in array) { jsonString = ToJson(item.ToString()) + ","; } return Json.DeleteLast(jsonString) + "]"; } /// <summary> /// 刪除開頭字符 /// </summary> /// <param name="str">須要刪除的字符</param> /// <returns>完成後的字符串</returns> private static string DeleteLast(string str) { if (str.Length > 1) { return str.Substring(0, str.Length - 1); } return str; } /// <summary> /// Datatable轉換為Json /// </summary> /// <param name="table">Datatable對象</param> /// <returns>Json字符串</returns> public static string ToJson(DataTable table) { string jsonString = "["; DataRowCollection drc = table.Rows; for (int i = 0; i < drc.Count; i++) { jsonString += "{"; foreach (DataColumn column in table.Columns) { jsonString += "\"" + ToJson(column.ColumnName) + "\":"; if (column.DataType == typeof(DateTime) || column.DataType == typeof(string)) { jsonString += "\"" + ToJson(drc[i][column.ColumnName].ToString()) + "\","; } else { jsonString += ToJson(drc[i][column.ColumnName].ToString()) + ","; } } jsonString = DeleteLast(jsonString) + "},"; } return DeleteLast(jsonString) + "]"; } /// <summary> /// DataReader轉換為Json /// </summary> /// <param name="dataReader">DataReader對象</param> /// <returns>Json字符串</returns> public static string ToJson(DbDataReader dataReader) { string jsonString = "["; while (dataReader.Read()) { jsonString += "{"; for (int i = 0; i < dataReader.FieldCount; i++) { jsonString += "\"" + ToJson(dataReader.GetName(i)) + "\":"; if (dataReader.GetFieldType(i) == typeof(DateTime) || dataReader.GetFieldType(i) == typeof(string)) { jsonString += "\"" + ToJson(dataReader[i].ToString()) + "\","; } else { jsonString += ToJson(dataReader[i].ToString()) + ","; } } jsonString = DeleteLast(jsonString) + "}"; } dataReader.Close(); return DeleteLast(jsonString) + "]"; } /// <summary> /// DataSet轉換為Json /// </summary> /// <param name="dataSet">DataSet對象</param> /// <returns>Json字符串</returns> public static string ToJson(DataSet dataSet) { string jsonString = "{"; foreach (DataTable table in dataSet.Tables) { jsonString += "\"" + ToJson(table.TableName) + "\":" + ToJson(table) + ","; } return jsonString = DeleteLast(jsonString) + "}"; } /// <summary> /// String轉換為Json /// </summary> /// <param name="value">String對象</param> /// <returns>Json字符串</returns> public static string ToJson(string value) { if (string.IsNullOrEmpty(value)) { return string.Empty; } string temstr; temstr = value; temstr = temstr.WordStr("{", "{").WordStr("}", "}").WordStr(":", ":").WordStr(",", ",").WordStr("[", "【").WordStr("]", "】").WordStr(";", ";").WordStr("\n", "<br/>").WordStr("\r", ""); temstr = temstr.WordStr("\t", " "); temstr = temstr.WordStr("'", "\'"); temstr = temstr.WordStr(@"\", @"\\"); temstr = temstr.WordStr("\"", "\"\""); return temstr; }
願望本文所述對年夜家C#法式設計有所贊助。