using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; namespace Edu.Common.Plugin { /// /// Json帮助类 /// public static class JsonHelper { /// /// 获取String /// /// /// /// public static string GetStringValue(this JObject jObject, string key) { return jObject[key] == null ? "" : jObject[key].ToString(); } /// /// 获取日期时间 /// /// /// /// public static DateTime GetDateTime(this JObject jObject, string key) { DateTime.TryParse(jObject[key] == null ? "" : jObject[key].ToString(), out DateTime result); return result; } /// /// 获取Int /// /// /// /// public static int GetInt(this JObject jObject, string key) { Int32.TryParse(jObject[key] == null ? "" : jObject[key].ToString(), out int result); return result; } /// /// 获取Int /// /// /// /// public static long GetLong(this JObject jObject, string key) { long.TryParse(jObject[key] == null ? "" : jObject[key].ToString(), out long result); return result; } /// /// 获取当前页 /// /// /// /// 当前页 /// public static int GetPageIndex(this JObject jObject, string key, int pageIndex = 1) { Int32.TryParse(jObject[key] == null ? "" : jObject[key].ToString(), out int result); return result == 0 ? pageIndex : result; } /// /// 获取分页大小 /// /// /// /// 当前页 /// public static int GetPageSize(this JObject jObject, string key, int pageSize = 15) { Int32.TryParse(jObject[key] == null ? "" : jObject[key].ToString(), out int result); return result == 0 ? pageSize : result; } /// /// 获取如果值为空,则取默认值 /// /// /// /// /// public static int GetInt(this JObject jObject, string key, int defalutvalue) { int result; if (jObject[key] == null) { result = defalutvalue; } else { Int32.TryParse(jObject[key] == null ? "" : jObject[key].ToString(), out result); } return result; } /// /// 获取Decimal /// /// /// /// public static decimal GetDecimal(this JObject jObject, string key) { decimal.TryParse(jObject[key] == null ? "" : jObject[key].ToString(), out decimal result); return result; } /// /// 获取Double /// /// /// /// public static double GetDouble(this JObject jObject, string key) { double.TryParse(jObject[key] == null ? "" : jObject[key].ToString(), out double result); return result; } /// /// 获取Bool /// /// /// /// public static bool GetBoolValue(this JObject jObject, string key) { bool.TryParse(jObject[key] == null ? "false" : jObject[key].ToString(), out bool result); return result; } /// /// 序列化 /// /// /// public static string Serialize(this object obj) { IsoDateTimeConverter timeFormat = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }; return Newtonsoft.Json.JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented, timeFormat); } /// /// 反序列化 /// /// 类型 /// 值 /// T public static T DeserializeObject(this string value) where T : class { JsonSerializerSettings jsSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; return Newtonsoft.Json.JsonConvert.DeserializeObject(value, jsSetting); } #region Json 字符串 转换为 DataTable数据集合 /// /// Json 字符串 转换为 DataTable数据集合 /// /// /// public static DataTable ToDataTable(string json) { DataTable dataTable = new DataTable(); //实例化 DataTable result; try { ArrayList arrayList = JsonConvert.DeserializeObject(json); if (arrayList.Count > 0) { foreach (Dictionary dictionary in arrayList) { if (dictionary.Keys.Count() == 0) { result = dataTable; return result; } if (dataTable.Columns.Count == 0) { foreach (string current in dictionary.Keys) { var obj = dictionary[current]; dataTable.Columns.Add(current, typeof(string) /*obj == null ? typeof(string) : obj.GetType()*/);//全当做字符串处理 } } DataRow dataRow = dataTable.NewRow(); foreach (string current in dictionary.Keys) { dataRow[current] = dictionary[current]; } dataTable.Rows.Add(dataRow); //循环添加行到DataTable中 } } } catch { } result = dataTable; return result; } #endregion /// /// 对象命名小驼峰式转换 /// /// public static object GetCamelCaseResultJson(object data) { if (data == null) { return null; } //json对象命名小驼峰式转换 var json = JsonConvert.SerializeObject( data, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() } ); return JsonConvert.DeserializeObject(json); } /// /// 判断字符串是不是JSON格式 /// /// /// public static bool IsJson(string json) { bool flag = false; if (!string.IsNullOrEmpty(json)) { try { _ = (JObject)JsonConvert.DeserializeObject(json); flag = true; } catch { flag = false; } } return flag; } } }