JsonObjectHelper.cs 6.1 KB

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