| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /******************************************************************************
- * 文件名称: JsonHelper.cs
- * 文件说明: Json业务的封装,处理JSON串的封装,解析等
- * 当前版本: V1.0
- * 创建日期: 2022-04-14
- *
- * 2020-04-14: 增加 getIrisServiceInparamBaseJson 方法
- * 2020-04-14: 增加 getIrisServiceInparamBaseJson(重载) 方法
- * 2020-04-14: 增加 getIrisExceptionJson 方法
- * 2020-04-14: 增加 parseBusinessJson 方法
- ******************************************************************************/
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MedicalInsurance.Common;
- using System.IO;
- namespace MedicalInsurance.Helper
- {
- class JsonHelper
- {
- /// <summary>
- /// 获取iris服务的基本JSON串格式(只有一个入参)
- /// </summary>
- /// <param name="code"></param>
- /// <param name="joParam"></param>
- /// <returns></returns>
- public static JObject getIrisServiceInparamBaseJson(string code, JObject joParam)
- {
- try
- {
- dynamic joInparam = new JObject();
- joInparam.code = code;
- dynamic joTmp = new JObject();
- JArray jaParam = new JArray();
- jaParam.Add(joParam);
- joInparam.Add("params", JArray.FromObject(jaParam));
- return joInparam;
- }
- catch (Exception ex)
- {
- return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:" , ex.Message);
- }
-
- }
- /// <summary>
- /// 获取iris服务的基本JSON串格式,有多个入参
- /// </summary>
- /// <param name="code"></param>
- /// <param name="jaParams"></param>
- /// <returns></returns>
- public static JObject getIrisServiceInparamBaseJson(string code, JArray jaParams)
- {
- try
- {
- dynamic joInparam = new JObject();
- joInparam.code = code;
- joInparam.Add("params", JArray.FromObject(jaParams));
- return joInparam;
- }
- catch (Exception ex)
- {
- return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:" , ex.Message);
- }
- }
- /// <summary>
- /// 获取iris服务的基本JSON串格式(带Session)
- /// </summary>
- /// <param name="code"></param>
- /// <param name="joParam"></param>
- /// <returns></returns>
- public static JObject getIrisServiceInparamBaseJson(string code, JObject joParam,JArray jaSession)
- {
- try
- {
- dynamic joInparam = new JObject();
- joInparam.code = code;
- dynamic joTmp = new JObject();
- JArray jaParam = new JArray();
- jaParam.Add(joParam);
- joInparam.Add("params", JArray.FromObject(jaParam));
- joInparam.Add("session", JArray.FromObject(jaSession));
- return joInparam;
- }
- catch (Exception ex)
- {
- return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:", ex.Message);
- }
- }
- /// <summary>
- /// 组织异常JSON串
- /// </summary>
- /// <param name="errorCode"></param>
- /// <param name="errorMessage"></param>
- /// <returns></returns>
- public static JObject getIrisExceptionJson(int errorCode, string eventName,string errorMessage)
- {
- dynamic joRtn = new JObject();
- joRtn.errorCode = -1;
- joRtn.errorMessage = eventName + "提示:" + errorMessage;
- return joRtn;
- }
- /// <summary>
- /// 解析业务JSON串(业务串固定格式为{errorCode = ,errorMessage = ,result ={}})
- /// </summary>
- /// <param name="json"></param>
- /// <param name="errorMsg"></param>
- /// <returns></returns>
- public static int parseBusinessJson(JObject json,out string errorMsg)
- {
- try
- {
- errorMsg = "";
- if (int.Parse(json["errorCode"].ToString()) != 0)
- {
- errorMsg = json["errorMessage"].ToString();
- }
- return int.Parse(json["errorCode"].ToString());
- }
- catch (Exception ex)
- {
- errorMsg = ex.Message;
- return -1;
- }
- }
- /// <summary>
- /// 解析业务JSON串(业务串固定格式为{errorCode = ,errorMessage = ,result ={}})
- /// </summary>
- /// <param name="json"></param>
- /// <param name="errorMsg"></param>
- /// <returns></returns>
- public static int parseCenterReturnJson(JObject json, out string errorMsg)
- {
- try
- {
- errorMsg = "";
- if (int.Parse(getJsonValue(json, "infcode")) != 0)
- {
- errorMsg = json["err_msg"].ToString();
- }
- return int.Parse(getJsonValue(json, "infcode"));
- }
- catch (Exception ex)
- {
- errorMsg = ex.Message;
- return -1;
- }
- }
- public static JObject getIrisReturnJson(int errorCode, string errorMessage, JObject joResult)
- {
- try
- {
- dynamic joRtn = new JObject();
- joRtn.errorCode = errorCode;
- joRtn.errorMessage = errorMessage;
- joRtn.Add("result", joResult);
- return joRtn;
- }
- catch (Exception ex)
- {
- return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:", ex.Message);
- }
- }
- /// <summary>
- /// 根据JSonPath查找节点值,如果节点不存在则返回空值
- /// </summary>
- /// <param name="jo"></param>
- /// <param name="jsonPath"></param>
- /// <returns></returns>
- public static string getJsonValue(JObject jo, string jsonPath)
- {
- JToken jt = jo.SelectToken("$." + jsonPath);
- if (jt != null)
- {
- return jt.ToString();
- }
- else
- {
- GlobalVariables.writeLog(jsonPath + "属性值为空");
- return "";
- }
- }
- /// <summary>
- /// 压缩JSON,占用体积减小(重载)
- /// </summary>
- /// <param name="json"></param>
- /// <returns></returns>
- public static string Compress(string json)
- {
- StringBuilder sb = new StringBuilder();
- using (StringReader reader = new StringReader(json))
- {
- int ch = -1;
- int lastch = -1;
- bool isQuoteStart = false;
- while ((ch = reader.Read()) > -1)
- {
- if ((char)lastch != '\\' && (char)ch == '\"')
- {
- if (!isQuoteStart)
- {
- isQuoteStart = true;
- }
- else
- {
- isQuoteStart = false;
- }
- }
- if (!Char.IsWhiteSpace((char)ch) || isQuoteStart)
- {
- sb.Append((char)ch);
- }
- lastch = ch;
- }
- }
- return sb.ToString();
- }
- }
- }
|