JsonHelper.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /******************************************************************************
  2. * 文件名称: JsonHelper.cs
  3. * 文件说明: Json业务的封装,处理JSON串的封装,解析等
  4. * 当前版本: V1.0
  5. * 创建日期: 2022-04-14
  6. *
  7. * 2020-04-14: 增加 getIrisServiceInparamBaseJson 方法
  8. * 2020-04-14: 增加 getIrisServiceInparamBaseJson(重载) 方法
  9. * 2020-04-14: 增加 getIrisExceptionJson 方法
  10. * 2020-04-14: 增加 parseBusinessJson 方法
  11. ******************************************************************************/
  12. using Newtonsoft.Json.Linq;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using MedicalInsurance.Common;
  19. using System.IO;
  20. namespace MedicalInsurance.Helper
  21. {
  22. class JsonHelper
  23. {
  24. /// <summary>
  25. /// 获取iris服务的基本JSON串格式(只有一个入参)
  26. /// </summary>
  27. /// <param name="code"></param>
  28. /// <param name="joParam"></param>
  29. /// <returns></returns>
  30. public static JObject getIrisServiceInparamBaseJson(string code, JObject joParam)
  31. {
  32. try
  33. {
  34. dynamic joInparam = new JObject();
  35. joInparam.code = code;
  36. dynamic joTmp = new JObject();
  37. JArray jaParam = new JArray();
  38. jaParam.Add(joParam);
  39. joInparam.Add("params", JArray.FromObject(jaParam));
  40. return joInparam;
  41. }
  42. catch (Exception ex)
  43. {
  44. return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:" , ex.Message);
  45. }
  46. }
  47. /// <summary>
  48. /// 获取iris服务的基本JSON串格式,有多个入参
  49. /// </summary>
  50. /// <param name="code"></param>
  51. /// <param name="jaParams"></param>
  52. /// <returns></returns>
  53. public static JObject getIrisServiceInparamBaseJson(string code, JArray jaParams)
  54. {
  55. try
  56. {
  57. dynamic joInparam = new JObject();
  58. joInparam.code = code;
  59. joInparam.Add("params", JArray.FromObject(jaParams));
  60. return joInparam;
  61. }
  62. catch (Exception ex)
  63. {
  64. return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:" , ex.Message);
  65. }
  66. }
  67. /// <summary>
  68. /// 获取iris服务的基本JSON串格式(带Session)
  69. /// </summary>
  70. /// <param name="code"></param>
  71. /// <param name="joParam"></param>
  72. /// <returns></returns>
  73. public static JObject getIrisServiceInparamBaseJson(string code, JObject joParam,JArray jaSession)
  74. {
  75. try
  76. {
  77. dynamic joInparam = new JObject();
  78. joInparam.code = code;
  79. dynamic joTmp = new JObject();
  80. JArray jaParam = new JArray();
  81. jaParam.Add(joParam);
  82. joInparam.Add("params", JArray.FromObject(jaParam));
  83. joInparam.Add("session", JArray.FromObject(jaSession));
  84. return joInparam;
  85. }
  86. catch (Exception ex)
  87. {
  88. return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:", ex.Message);
  89. }
  90. }
  91. /// <summary>
  92. /// 组织异常JSON串
  93. /// </summary>
  94. /// <param name="errorCode"></param>
  95. /// <param name="errorMessage"></param>
  96. /// <returns></returns>
  97. public static JObject getIrisExceptionJson(int errorCode, string eventName,string errorMessage)
  98. {
  99. dynamic joRtn = new JObject();
  100. joRtn.errorCode = -1;
  101. joRtn.errorMessage = eventName + "提示:" + errorMessage;
  102. return joRtn;
  103. }
  104. /// <summary>
  105. /// 解析业务JSON串(业务串固定格式为{errorCode = ,errorMessage = ,result ={}})
  106. /// </summary>
  107. /// <param name="json"></param>
  108. /// <param name="errorMsg"></param>
  109. /// <returns></returns>
  110. public static int parseBusinessJson(JObject json,out string errorMsg)
  111. {
  112. try
  113. {
  114. errorMsg = "";
  115. if (int.Parse(json["errorCode"].ToString()) != 0)
  116. {
  117. errorMsg = json["errorMessage"].ToString();
  118. }
  119. return int.Parse(json["errorCode"].ToString());
  120. }
  121. catch (Exception ex)
  122. {
  123. errorMsg = ex.Message;
  124. return -1;
  125. }
  126. }
  127. /// <summary>
  128. /// 解析业务JSON串(业务串固定格式为{errorCode = ,errorMessage = ,result ={}})
  129. /// </summary>
  130. /// <param name="json"></param>
  131. /// <param name="errorMsg"></param>
  132. /// <returns></returns>
  133. public static int parseCenterReturnJson(JObject json, out string errorMsg)
  134. {
  135. try
  136. {
  137. errorMsg = "";
  138. if (int.Parse(getJsonValue(json, "infcode")) != 0)
  139. {
  140. errorMsg = json["err_msg"].ToString();
  141. }
  142. return int.Parse(getJsonValue(json, "infcode"));
  143. }
  144. catch (Exception ex)
  145. {
  146. errorMsg = ex.Message;
  147. return -1;
  148. }
  149. }
  150. public static JObject getIrisReturnJson(int errorCode, string errorMessage, JObject joResult)
  151. {
  152. try
  153. {
  154. dynamic joRtn = new JObject();
  155. joRtn.errorCode = errorCode;
  156. joRtn.errorMessage = errorMessage;
  157. joRtn.Add("result", joResult);
  158. return joRtn;
  159. }
  160. catch (Exception ex)
  161. {
  162. return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:", ex.Message);
  163. }
  164. }
  165. /// <summary>
  166. /// 根据JSonPath查找节点值,如果节点不存在则返回空值
  167. /// </summary>
  168. /// <param name="jo"></param>
  169. /// <param name="jsonPath"></param>
  170. /// <returns></returns>
  171. public static string getJsonValue(JObject jo, string jsonPath)
  172. {
  173. JToken jt = jo.SelectToken("$." + jsonPath);
  174. if (jt != null)
  175. {
  176. return jt.ToString();
  177. }
  178. else
  179. {
  180. GlobalVariables.writeLog(jsonPath + "属性值为空");
  181. return "";
  182. }
  183. }
  184. /// <summary>
  185. /// 压缩JSON,占用体积减小(重载)
  186. /// </summary>
  187. /// <param name="json"></param>
  188. /// <returns></returns>
  189. public static string Compress(string json)
  190. {
  191. StringBuilder sb = new StringBuilder();
  192. using (StringReader reader = new StringReader(json))
  193. {
  194. int ch = -1;
  195. int lastch = -1;
  196. bool isQuoteStart = false;
  197. while ((ch = reader.Read()) > -1)
  198. {
  199. if ((char)lastch != '\\' && (char)ch == '\"')
  200. {
  201. if (!isQuoteStart)
  202. {
  203. isQuoteStart = true;
  204. }
  205. else
  206. {
  207. isQuoteStart = false;
  208. }
  209. }
  210. if (!Char.IsWhiteSpace((char)ch) || isQuoteStart)
  211. {
  212. sb.Append((char)ch);
  213. }
  214. lastch = ch;
  215. }
  216. }
  217. return sb.ToString();
  218. }
  219. }
  220. }