using System; using System.Collections.Generic; using System.Dynamic; using System.Xml; using IwbZero.ToolCommon.LogHelpers; using Newtonsoft.Json; using Newtonsoft.Json.Converters; namespace IwbZero.ToolCommon.StringModel { public static class JsonObjectHelper { #region Json public static string Obj2String(this object source) { try { return JsonConvert.SerializeObject(source); } catch (Exception e) { typeof(JsonObjectHelper).LogError(e); return ""; } } public static string Obj2StringCamelCase(this object source) { return Obj2String(source, true); } public static string Obj2String(this object source,bool isCamelCase) { try { var setting = new JsonSerializerSettings { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() }; return JsonConvert.SerializeObject(source,setting); } catch (Exception e) { typeof(JsonObjectHelper).LogError(e); return ""; } } public static T Str2Obj(this string source) { return GetModel(source); } public static T GetModel(this string source) { try { if (string.IsNullOrEmpty(source)) { return default(T); } return JsonConvert.DeserializeObject(source); } catch (Exception e) { typeof(JsonObjectHelper).LogError(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) { typeof(JsonObjectHelper).LogError(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) { typeof(JsonObjectHelper).LogError(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.IsNotEmpty()) { 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 /// /// 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 } } }