C#自定义将各种对象转换成JSON格式的类
程序员文章站
2022-04-15 12:06:15
...
这个C#封装类可以用于将各种格式的数据转换成JSON格式,包括List转换成Json,普通集合转换Json ,DataSet转换为Json
,Datatable转换为Json ,DataReader转换为Json等,如果你需要将对象转换成JSON,可以使用这个类。
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Reflection; using System.Collections; using System.Data.Common; namespace DotNet.Utilities { //JSON转换类 public class ConvertJson { #region 私有方法 ////// 过滤特殊字符 /// private static string String2Json(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0; i /// 格式化字符型、日期型、布尔型 /// private static string StringFormat(string str, Type type) { if (type == typeof(string)) { str = String2Json(str); str = "\"" + str + "\""; } else if (type == typeof(DateTime)) { str = "\"" + str + "\""; } else if (type == typeof(bool)) { str = str.ToLower(); } else if (type != typeof(string) && string.IsNullOrEmpty(str)) { str = "\"" + str + "\""; } return str; } #endregion #region List转换成Json ////// List转换成Json /// public static string ListToJson(IList list) { object obj = list[0]; return ListToJson (list, obj.GetType().Name); } /// /// List转换成Json /// public static string ListToJson(IList list, string jsonName) { StringBuilder Json = new StringBuilder(); if (string.IsNullOrEmpty(jsonName)) jsonName = list[0].GetType().Name; Json.Append("{\"" + jsonName + "\":["); if (list.Count > 0) { for (int i = 0; i (); PropertyInfo[] pi = obj.GetType().GetProperties(); Json.Append("{"); for (int j = 0; j /// 对象转换为Json /// /// 对象 /// Json字符串 public static string ToJson(object jsonObject) { string jsonString = "{"; PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties(); for (int i = 0; i /// 对象集合转换Json /// /// 集合对象 ///Json字符串 public static string ToJson(IEnumerable array) { string jsonString = "["; foreach (object item in array) { jsonString += ToJson(item) + ","; } jsonString.Remove(jsonString.Length - 1, jsonString.Length); return jsonString + "]"; } #endregion #region 普通集合转换Json ////// 普通集合转换Json /// /// 集合对象 ///Json字符串 public static string ToArrayString(IEnumerable array) { string jsonString = "["; foreach (object item in array) { jsonString = ToJson(item.ToString()) + ","; } jsonString.Remove(jsonString.Length - 1, jsonString.Length); return jsonString + "]"; } #endregion #region DataSet转换为Json ////// DataSet转换为Json /// /// DataSet对象 ///Json字符串 public static string ToJson(DataSet dataSet) { string jsonString = "{"; foreach (DataTable table in dataSet.Tables) { jsonString += "\"" + table.TableName + "\":" + ToJson(table) + ","; } jsonString = jsonString.TrimEnd(','); return jsonString + "}"; } #endregion #region Datatable转换为Json ////// Datatable转换为Json /// /// Datatable对象 ///Json字符串 public static string ToJson(DataTable dt) { StringBuilder jsonString = new StringBuilder(); jsonString.Append("["); DataRowCollection drc = dt.Rows; for (int i = 0; i /// DataTable转换为Json /// public static string ToJson(DataTable dt, string jsonName) { StringBuilder Json = new StringBuilder(); if (string.IsNullOrEmpty(jsonName)) jsonName = dt.TableName; Json.Append("{\"" + jsonName + "\":["); if (dt.Rows.Count > 0) { for (int i = 0; i /// DataReader转换为Json /// /// DataReader对象 ///Json字符串 public static string ToJson(DbDataReader dataReader) { StringBuilder jsonString = new StringBuilder(); jsonString.Append("["); while (dataReader.Read()) { jsonString.Append("{"); for (int i = 0; i