/****************************************************************************** * 文件名称: 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 PTMedicalInsurance.Common; using System.IO; using PTMedicalInsurance.Variables; namespace PTMedicalInsurance.Helper { class JsonHelper { /// /// 获取iris服务的基本JSON串格式(只有一个入参) /// /// /// /// 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); } } /// /// 获取iris服务的基本JSON串格式,有多个入参 /// /// /// /// 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); } } /// /// 组织异常JSON串 /// /// /// /// public static JObject getIrisExceptionJson(int errorCode, string eventName,string errorMessage) { dynamic joRtn = new JObject(); joRtn.errorCode = -1; joRtn.errorMessage = eventName + "提示:" + errorMessage; return joRtn; } /// /// 解析业务JSON串(业务串固定格式为{errorCode = ,errorMessage = ,result ={}}) /// /// /// /// public static int parseBusinessJson(JObject json,out string errorMsg) { try { errorMsg = ""; if (json.Property("errorMessage") != null) { errorMsg = json["errorMessage"].ToString(); } return int.Parse(json["errorCode"].ToString()); } catch (Exception ex) { errorMsg = ex.Message; return -1; } } public static int parseCenterJson(JObject json, out string errorMsg) { try { errorMsg = ""; if (int.Parse(json["infcode"].ToString()) != 0) { errorMsg = json["err_msg"].ToString(); } return int.Parse(json["infcode"].ToString()); } 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", JObject.FromObject(joResult)); return joRtn; } catch (Exception ex) { return getIrisExceptionJson(-1, "getIrisServiceInparamBaseJson:", ex.Message); } } /// /// 根据JSonPath查找节点值,如果节点不存在则返回空值 /// /// /// /// public static string getJsonValue(JObject jo, string jsonPath) { JToken jt = jo.SelectToken("$." + jsonPath); if (jt != null) { return jt.ToString(); } else { GlobalVariables.writeLog(jsonPath + "属性值为空"); return ""; } } /****************************************************************下载工具用********************************************************************************/ /// /// 根据JSonPath查找节点值,如果节点不存在则返回空值 /// /// /// /// public static string getDestValue(JObject jo, string destPath) { JToken jt = jo.SelectToken("$." + destPath); if (jt != null) { return jt.ToString(); } else { Global.writeLog(destPath + "的JToken属性值为空"); return ""; } } public static string getDestProperty(JObject jo, string propertyName) { JProperty jp = jo.Property(propertyName); if (jp != null) { string jpStr = jp.ToString(); string rtnResult = "{ " + jpStr + "}"; return rtnResult; } else { Global.writeLog(propertyName + "的JProperty属性值为空"); return ""; } } /// /// 压缩JSON,占用体积减小(重载) /// /// /// 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(); } public static string Compress(JObject jsonObj) { //https://blog.csdn.net/yangjiaosun/article/details/103717256 return Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj); } /// /// 组织异常JSON串 /// /// /// /// public static JObject setExceptionJson(int errorCode, string eventName, string errorMessage) { dynamic joRtn = new JObject(); joRtn.errorCode = errorCode; joRtn.errorMessage = eventName + "提示:" + errorMessage; return joRtn; } /// /// 组织中心入参 /// /// /// /// public static string setCenterInpar(string infno, string input) { dynamic Jo = new JObject(); Jo.infno = infno; Global.curEvt.msgid = Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff"); Jo.msgid = Global.curEvt.msgid; Jo.mdtrtarea_admvs = Global.inf.areaCode; Jo.insuplc_admdvs = Global.pat.insuplc_admdvs; Jo.recer_sys_code = Global.inf.recivedSystem; Jo.dev_safe_info = Global.inf.dev_safe_info; //设备编号 Jo.cainfo = Global.inf.cainfo;//设备安全信息 Jo.signtype = Global.inf.signatureType; ; Jo.infver = Global.inf.version; ; Jo.opter_type = Global.user.type; ; Jo.opter = Global.user.ID; ; Jo.opter_name = Global.user.name; ; Jo.inf_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); Jo.fixmedins_code = Global.inf.hospitalNO; Jo.fixmedins_name = Global.inf.hospitalName; Jo.sign_no = Global.curEvt.signno; JObject joInput = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(input); Jo.Add("input", JObject.FromObject(joInput)); return Jo.ToString(); } /// /// 组织中心入参 /// /// /// /// public static string setCenterInpar(string infno, JObject joInput) { dynamic Jo = new JObject(); Jo.infno = infno; Global.curEvt.msgid = Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff"); Jo.msgid = Global.curEvt.msgid; Jo.mdtrtarea_admvs = Global.inf.areaCode; Jo.insuplc_admdvs = Global.pat.insuplc_admdvs; Jo.recer_sys_code = Global.inf.recivedSystem; Jo.dev_no = Global.inf.dev_no; Jo.dev_safe_info = Global.inf.dev_safe_info; //设备编号 Jo.cainfo = Global.inf.cainfo;//设备安全信息 Jo.signtype = Global.inf.signatureType; ; Jo.infver = Global.inf.version; ; Jo.opter_type = Global.user.type; ; Jo.opter = Global.user.ID; ; Jo.opter_name = Global.user.name; ; Jo.inf_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); Jo.fixmedins_code = Global.inf.hospitalNO; Jo.fixmedins_name = Global.inf.hospitalName; Jo.sign_no = Global.curEvt.signno; Jo.Add("input", joInput); return Jo.ToString(); } /// /// 组织Iris入参 /// /// /// /// public static JObject setIrisInpar(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)); joInparam.Add("session", Global.curEvt.jaSession); return joInparam; } catch (Exception ex) { return setExceptionJson(-1, "setIrisInpar:", ex.Message); } } /// /// 组织Iris入参 /// /// /// /// public static JObject setIrisInpar(string code, JArray jaParams) { try { dynamic joInparam = new JObject(); joInparam.code = code; dynamic joTmp = new JObject(); joInparam.Add("params", jaParams); joInparam.Add("session", Global.curEvt.jaSession); return joInparam; } catch (Exception ex) { return setExceptionJson(-1, "setIrisInpar:", ex.Message); } } /// /// 解析中心返参 /// /// /// /// public static int parseCenterRtnValue(JObject joRtn, out string errorMsg) { try { errorMsg = getDestValue(joRtn, "err_msg"); return int.Parse(getDestValue(joRtn, "infcode")); } catch (Exception ex) { errorMsg = "解析中心返参发生异常:" + ex.Message; return -1; } } /// /// 组织IRIS返参 /// /// /// /// /// public static JObject setIrisReturnValue(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 setExceptionJson(-1, "setIrisReturnValue:", ex.Message); } } /// /// 解析IRIS返参 /// /// /// /// public static int parseIrisRtnValue(JObject joRtn, out string errorMsg) { try { errorMsg = getDestValue(joRtn, "errorMessage"); return int.Parse(getDestValue(joRtn, "errorCode")); } catch (Exception ex) { errorMsg = "解析Iris返参发生异常:" + ex.Message; return -1; } } } }