using Abp.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; using System.Dynamic; using System.Xml; namespace VberZero.Tools.StringModel; public static class JsonObjectHelper { #region Json public static string Obj2String(this object source, bool isCamelCase = true) { if (source == null) { return ""; } try { if (isCamelCase) { var serializerSettings = new JsonSerializerSettings { // 设置为驼峰命名 ContractResolver = new CamelCasePropertyNamesContractResolver() }; return JsonConvert.SerializeObject(source, serializerSettings); } return JsonConvert.SerializeObject(source); } catch (Exception e) { LogHelper.LogException(e); return ""; } } public static T Str2Obj(this string source) { return GetModel(source); } public static T Map(this object source) { if (source == null) { return default; } string str = Obj2String(source); if (string.IsNullOrEmpty(str)) { return default; } return GetModel(str); } public static T GetModel(this string source) { try { if (string.IsNullOrEmpty(source)) { return default(T); } return JsonConvert.DeserializeObject(source); } catch (Exception e) { LogHelper.LogException(e); return default(T); } } public static T GetObj(this string source, string key = null) { try { var converter = new ExpandoObjectConverter(); var jsonObject = JsonConvert.DeserializeObject(source, converter); if (string.IsNullOrEmpty(key)) { T info = JsonConvert.DeserializeObject(source); return info; } object obj = jsonObject.GetValue(key); return (T)obj; } catch (Exception e) { LogHelper.LogException(e); return default(T); } } public static T GetResultModel(this string objStr, bool isLower = true) { try { var converter = new ExpandoObjectConverter(); dynamic jsonObject = JsonConvert.DeserializeObject(objStr, converter); if (isLower) { if (jsonObject.success) { T info = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(jsonObject.result)); return info; } } else { if (jsonObject.Success) { T info = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(jsonObject.Result)); return info; } } } catch (Exception e) { LogHelper.LogException(e); } return default(T); } #endregion Json #region XML /// /// 返回指定节点下信息的JSON格式字符串 /// /// xml字符串 /// 节点名称,应从根节点开始 /// public static string Xml2Json(string str, string nodeName) { XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(str); XmlNode node = xDoc.SelectSingleNode(nodeName); var result = JsonConvert.SerializeXmlNode(node); return result; } /// /// 返回指定节点下信息的JSON格式字符串 /// /// xml /// 节点名称,应从根节点开始 /// public static string Xml2Json(this XmlNode node, string nodeName = null) { if (nodeName != null && nodeName.NotEmpty()) { node = node.SelectSingleNode(nodeName); } string result = JsonConvert.SerializeXmlNode(node); return result; } /// /// json转成xml /// /// /// public static string Json2Xml(string str) { XmlDocument xml = JsonConvert.DeserializeXmlNode(str); string result = xml.OuterXml; return result; } #endregion XML /// /// Gets the value. /// /// The object. /// The key. /// System.Object. public static dynamic GetValue(this object obj, string key) { if (obj is List oos) { foreach (var oo in oos) { var result = GetValue(oo, key);//递归返回匹配的值 if (result != null) return result; } } else if (obj is object[] objects) { foreach (var oo in objects) { var result = GetValue(oo, key);//递归返回匹配的值 if (result != null) return result; } } else if (obj is IDictionary dictionarys) { foreach (var dic in dictionarys) { if (dic.Key == key || dic.Key == key.ToLower() || dic.Key == key.ToUpper()) return dic.Value; } //如果上面的遍历没有结果,则该值可能嵌套在property.Value里面,需要递归解析 foreach (var dic in dictionarys) { var result = GetValue(dic.Value, key);//递归返回匹配的值 if (result != null) return result; } } return null;//没有匹配值,返回null } }