JsonObjectHelper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using Abp.Logging;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Converters;
  4. using Newtonsoft.Json.Serialization;
  5. using System.Dynamic;
  6. using System.Xml;
  7. namespace VberZero.Tools.StringModel;
  8. public static class JsonObjectHelper
  9. {
  10. #region Json
  11. public static string Obj2String(this object source, bool isCamelCase = true)
  12. {
  13. if (source == null)
  14. {
  15. return "";
  16. }
  17. try
  18. {
  19. if (isCamelCase)
  20. {
  21. var serializerSettings = new JsonSerializerSettings
  22. {
  23. // 设置为驼峰命名
  24. ContractResolver = new CamelCasePropertyNamesContractResolver()
  25. };
  26. return JsonConvert.SerializeObject(source, serializerSettings);
  27. }
  28. return JsonConvert.SerializeObject(source);
  29. }
  30. catch (Exception e)
  31. {
  32. LogHelper.LogException(e);
  33. return "";
  34. }
  35. }
  36. public static T Str2Obj<T>(this string source)
  37. {
  38. return GetModel<T>(source);
  39. }
  40. public static T Map<T>(this object source)
  41. {
  42. if (source == null)
  43. {
  44. return default;
  45. }
  46. string str = Obj2String(source);
  47. if (string.IsNullOrEmpty(str))
  48. {
  49. return default;
  50. }
  51. return GetModel<T>(str);
  52. }
  53. public static T GetModel<T>(this string source)
  54. {
  55. try
  56. {
  57. if (string.IsNullOrEmpty(source))
  58. {
  59. return default(T);
  60. }
  61. return JsonConvert.DeserializeObject<T>(source);
  62. }
  63. catch (Exception e)
  64. {
  65. LogHelper.LogException(e);
  66. return default(T);
  67. }
  68. }
  69. public static T GetObj<T>(this string source, string key = null)
  70. {
  71. try
  72. {
  73. var converter = new ExpandoObjectConverter();
  74. var jsonObject = JsonConvert.DeserializeObject<ExpandoObject>(source, converter);
  75. if (string.IsNullOrEmpty(key))
  76. {
  77. T info = JsonConvert.DeserializeObject<T>(source);
  78. return info;
  79. }
  80. object obj = jsonObject.GetValue(key);
  81. return (T)obj;
  82. }
  83. catch (Exception e)
  84. {
  85. LogHelper.LogException(e);
  86. return default(T);
  87. }
  88. }
  89. public static T GetResultModel<T>(this string objStr, bool isLower = true)
  90. {
  91. try
  92. {
  93. var converter = new ExpandoObjectConverter();
  94. dynamic jsonObject = JsonConvert.DeserializeObject<ExpandoObject>(objStr, converter);
  95. if (isLower)
  96. {
  97. if (jsonObject.success)
  98. {
  99. T info = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(jsonObject.result));
  100. return info;
  101. }
  102. }
  103. else
  104. {
  105. if (jsonObject.Success)
  106. {
  107. T info = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(jsonObject.Result));
  108. return info;
  109. }
  110. }
  111. }
  112. catch (Exception e)
  113. {
  114. LogHelper.LogException(e);
  115. }
  116. return default(T);
  117. }
  118. #endregion Json
  119. #region XML
  120. /// <summary>
  121. /// 返回指定节点下信息的JSON格式字符串
  122. /// </summary>
  123. /// <param name="str">xml字符串</param>
  124. /// <param name="nodeName">节点名称,应从根节点开始</param>
  125. /// <returns></returns>
  126. public static string Xml2Json(string str, string nodeName)
  127. {
  128. XmlDocument xDoc = new XmlDocument();
  129. xDoc.LoadXml(str);
  130. XmlNode node = xDoc.SelectSingleNode(nodeName);
  131. var result = JsonConvert.SerializeXmlNode(node);
  132. return result;
  133. }
  134. /// <summary>
  135. /// 返回指定节点下信息的JSON格式字符串
  136. /// </summary>
  137. /// <param name="node">xml</param>
  138. /// <param name="nodeName">节点名称,应从根节点开始</param>
  139. /// <returns></returns>
  140. public static string Xml2Json(this XmlNode node, string nodeName = null)
  141. {
  142. if (nodeName != null && nodeName.NotEmpty())
  143. {
  144. node = node.SelectSingleNode(nodeName);
  145. }
  146. string result = JsonConvert.SerializeXmlNode(node);
  147. return result;
  148. }
  149. /// <summary>
  150. /// json转成xml
  151. /// </summary>
  152. /// <param name="str"></param>
  153. /// <returns></returns>
  154. public static string Json2Xml(string str)
  155. {
  156. XmlDocument xml = JsonConvert.DeserializeXmlNode(str);
  157. string result = xml.OuterXml;
  158. return result;
  159. }
  160. #endregion XML
  161. /// <summary>
  162. /// Gets the value.
  163. /// </summary>
  164. /// <param name="obj">The object.</param>
  165. /// <param name="key">The key.</param>
  166. /// <returns>System.Object.</returns>
  167. public static dynamic GetValue(this object obj, string key)
  168. {
  169. if (obj is List<object> oos)
  170. {
  171. foreach (var oo in oos)
  172. {
  173. var result = GetValue(oo, key);//递归返回匹配的值
  174. if (result != null)
  175. return result;
  176. }
  177. }
  178. else if (obj is object[] objects)
  179. {
  180. foreach (var oo in objects)
  181. {
  182. var result = GetValue(oo, key);//递归返回匹配的值
  183. if (result != null)
  184. return result;
  185. }
  186. }
  187. else if (obj is IDictionary<string, object> dictionarys)
  188. {
  189. foreach (var dic in dictionarys)
  190. {
  191. if (dic.Key == key || dic.Key == key.ToLower() || dic.Key == key.ToUpper())
  192. return dic.Value;
  193. }
  194. //如果上面的遍历没有结果,则该值可能嵌套在property.Value里面,需要递归解析
  195. foreach (var dic in dictionarys)
  196. {
  197. var result = GetValue(dic.Value, key);//递归返回匹配的值
  198. if (result != null)
  199. return result;
  200. }
  201. }
  202. return null;//没有匹配值,返回null
  203. }
  204. }