中文和双引号会正常处理
DataTable实现了序列化, 反序列化,目前没有用到,所以没有实现
废话不说,直接拿去用吧
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Text.Json; using System.Text.Unicode; using System.Threading.Tasks; /* // 不序列化这个属性 [JsonIgnore] // 包含字段 [JsonInclude] // 自定义属性名 [JsonPropertyName("score")] */ namespace System { /// <summary> /// /// </summary> public static class JsonTextHelper { /// <summary></summary> public static JsonSerializerOptions SerializerOptions { get; set; } = new JsonSerializerOptions(); static JsonTextHelper() { // 添加自定义解析器 // SerializerOptions.Converters.Add(new MyDateTimeConverter()); // SerializerOptions.Converters.Add(new MyDateTimeNullConverter()); // 忽略大小写 SerializerOptions.PropertyNameCaseInsensitive = true; // 处理中文转义问题 SerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(UnicodeRanges.All); /* System.Text.Json 默认使用 Unicode 来进行安全的序列化, 因此将字串内的特殊符号编码成 Unicode,但这样可能有2个问题, 1.会造成 JSON 字串变大, 2." 字符,會变六個字符 u0022 */ // 转义不安全字符 SerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping; #if DEBUG // 缩进 SerializerOptions.WriteIndented = true; #endif } /// <summary> /// 序列化对象(可以是实体或实体集合) /// </summary> /// <param name="o">源字符串</param> /// <returns></returns> public static string SerializeObject(object o) { if (o is DataTable dt) { return serializeDataTable(dt); } string jsonStr = JsonSerializer.Serialize(o, SerializerOptions); return jsonStr; } #region 反序列化 /// <summary> /// 反序列化 /// </summary> /// <typeparam name="T">目标类型</typeparam> /// <param name="jsonStr">源字符串</param> /// <returns></returns> public static T DeserializeObject<T>(string jsonStr) { if (String.IsNullOrEmpty(jsonStr)) return default; try { return JsonSerializer.Deserialize<T>(jsonStr, SerializerOptions); } catch (Exception ex) { var s = jsonStr.Length > 300 ? 300 : jsonStr.Length; throw new Exception($"{jsonStr.Substring(0, s)} 反序列化异常!", ex); } } /// <summary> /// 反序列化jsonStr为实体集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonStr"></param> /// <returns></returns> public static IList<T> DeserializeList<T>(string jsonStr) where T : new() { if (String.IsNullOrEmpty(jsonStr)) return new List<T>(); try { return JsonSerializer.Deserialize<IList<T>>(jsonStr, SerializerOptions); } catch (Exception ex) { var s = jsonStr.Length > 300 ? 300 : jsonStr.Length; throw new Exception($"{jsonStr.Substring(0, s)} 反序列化异常!", ex); } } /// <summary> /// 传入Json数组字符串,解析为DataTable /// </summary> /// <param name="jsonStr"></param> /// <returns></returns> public static DataTable DeserializeDataTable(string jsonStr) { if (String.IsNullOrEmpty(jsonStr)) return new DataTable(); try { throw new Exception("暂时不支持!"); ////反序列化为实体 //JArray ja = JArray.Parse(jsonStr); //return JsonConvert.DeserializeObject<DataTable>(ja.ToString()); } catch (Exception ex) { var s = jsonStr.Length > 300 ? 300 : jsonStr.Length; throw new Exception($"{jsonStr.Substring(0, s)} 反序列化异常!", ex); } } /// <summary> /// /// </summary> /// <param name="jsonStr"></param> /// <returns></returns> public static IDictionary<String, Object> DeserializeDictionary(string jsonStr) { return JsonSerializer.Deserialize<IDictionary<string, object>>(jsonStr); } #endregion #region 辅助 private static String serializeDataTable(DataTable dt) { var cols = new List<String>(); foreach (DataColumn col in dt.Columns) { cols.Add(col.ColumnName); } var sb = new StringBuilder(); sb.Append("["); var count = dt.Rows.Count; for (var i = 0; i < count; i++) { var row = dt.Rows[i]; var dic = new Dictionary<String, Object>(); foreach (var col in cols) { dic.Add(col, row[col]); } var dicJson = JsonTextHelper.SerializeObject(dic); sb.Append(dicJson); if (i + 1 != count) { sb.Append(","); } } sb.Append("]"); return sb.ToString(); } #endregion } }