JsonObjectHelper.cs 7.0 KB

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