JsonHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. namespace DirectoryDownload.Helper
  19. {
  20. class JsonHelper
  21. {
  22. /// <summary>
  23. /// 获取iris服务的基本JSON串格式(只有一个入参)
  24. /// </summary>
  25. /// <param name="code"></param>
  26. /// <param name="joParam"></param>
  27. /// <returns></returns>
  28. public static JObject getIrisServiceInparamBaseJson(string code, JObject joParam)
  29. {
  30. try
  31. {
  32. dynamic joInparam = new JObject();
  33. joInparam.code = code;
  34. dynamic joTmp = new JObject();
  35. JArray jaParam = new JArray();
  36. jaParam.Add(joParam);
  37. joInparam.Add("params", JArray.FromObject(jaParam));
  38. return joInparam;
  39. }
  40. catch (Exception ex)
  41. {
  42. return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:", ex.Message);
  43. }
  44. }
  45. /// <summary>
  46. /// 获取iris服务的基本JSON串格式,有多个入参
  47. /// </summary>
  48. /// <param name="code"></param>
  49. /// <param name="jaParams"></param>
  50. /// <returns></returns>
  51. public static JObject getIrisServiceInparamBaseJson(string code, JArray jaParams)
  52. {
  53. try
  54. {
  55. dynamic joInparam = new JObject();
  56. joInparam.code = code;
  57. joInparam.Add("params", JArray.FromObject(jaParams));
  58. return joInparam;
  59. }
  60. catch (Exception ex)
  61. {
  62. return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:" , ex.Message);
  63. }
  64. }
  65. /// <summary>
  66. /// 组织异常JSON串
  67. /// </summary>
  68. /// <param name="errorCode"></param>
  69. /// <param name="errorMessage"></param>
  70. /// <returns></returns>
  71. public static JObject getIrisExceptionJson(int errorCode, string eventName,string errorMessage)
  72. {
  73. dynamic joRtn = new JObject();
  74. joRtn.errorCode = -1;
  75. joRtn.errorMessage = eventName + "提示:" + errorMessage;
  76. return joRtn;
  77. }
  78. /// <summary>
  79. /// 解析业务JSON串(业务串固定格式为{errorCode = ,errorMessage = ,result ={}})
  80. /// </summary>
  81. /// <param name="json"></param>
  82. /// <param name="errorMsg"></param>
  83. /// <returns></returns>
  84. public static int parseBusinessJson(JObject json,out string errorMsg)
  85. {
  86. try
  87. {
  88. errorMsg = "";
  89. if (json.Property("errorMessage") != null)
  90. {
  91. errorMsg = json["errorMessage"].ToString();
  92. }
  93. return int.Parse(json["errorCode"].ToString());
  94. }
  95. catch (Exception ex)
  96. {
  97. errorMsg = ex.Message;
  98. return -1;
  99. }
  100. }
  101. public static int parseCenterJson(JObject json, out string errorMsg)
  102. {
  103. try
  104. {
  105. errorMsg = "";
  106. if (int.Parse(json["infcode"].ToString()) != 0)
  107. {
  108. errorMsg = json["err_msg"].ToString();
  109. }
  110. return int.Parse(json["infcode"].ToString());
  111. }
  112. catch (Exception ex)
  113. {
  114. errorMsg = ex.Message;
  115. return -1;
  116. }
  117. }
  118. public static JObject getIrisReturnJson(int errorCode, string errorMessage, JObject joResult)
  119. {
  120. try
  121. {
  122. dynamic joRtn = new JObject();
  123. joRtn.errorCode = errorCode;
  124. joRtn.errorMessage = errorMessage;
  125. joRtn.Add("result", JObject.FromObject(joResult));
  126. return joRtn;
  127. }
  128. catch (Exception ex)
  129. {
  130. return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:", ex.Message);
  131. }
  132. }
  133. /// <summary>
  134. /// 根据JSonPath查找节点值,如果节点不存在则返回空值
  135. /// </summary>
  136. /// <param name="jo"></param>
  137. /// <param name="jsonPath"></param>
  138. /// <returns></returns>
  139. public static string getJsonValue(JObject jo, string jsonPath)
  140. {
  141. JToken jt = jo.SelectToken("$." + jsonPath);
  142. if (jt != null)
  143. {
  144. return jt.ToString();
  145. }
  146. else
  147. {
  148. GlobalVariables.writeLog(jsonPath + "属性值为空");
  149. return "";
  150. }
  151. }
  152. }
  153. }