Jsonhelper类
public static class JsonHelper { #region json转对象 /// <summary> /// json转对象 /// </summary> /// <param name="JsonText"></param> /// <returns></returns> public static T JsonToObject<T>(string JsonText) where T : class { if (string.IsNullOrWhiteSpace(JsonText)) { return null; } return JsonConvert.DeserializeObject<T>(JsonText); } public static List<T> JsonToListObject<T>(string JsonText) where T : class { if (string.IsNullOrWhiteSpace(JsonText)) { return null; } return JsonConvert.DeserializeObject<List<T>>(JsonText); } #endregion #region 对象转json /// <summary> /// 对象转json /// </summary> /// <param name="Object"></param> /// <returns></returns> public static string ObjectToJson(object Object) { if (Object == null) { return null; } return JsonConvert.SerializeObject(Object); } #endregion } }
上边需要添加nuget Newtonsoft.Json的v包 下边不需要
JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类 0成功 -1失败 ToJsonMy list = js.Deserialize<ToJsonMy>(str);
public void PostDZFP() { string SMBH = "913001012"; #region 赋值 DataTable dt = DbHelperOra.Query("SELECT (SELECT XINGM FROM GLASS_HUIYXX WHERE HUIYBH=A.HUIYBH) XINGM,A.SHOUKDBH,A.SHISJE JINE,MONEY2CHINESE(A.SHISJE) DXJE,(SELECT XINGM FROM GLASS_USER WHERE BIANH=A.SHOUKR) CAOZY FROM GLASS_SHOUKD A WHERE SHOUKDBH='" + SKDBM + "'").Tables[0]; DataTable dataitems = DbHelperOra.Query("SELECT A.SHANGPBH SP,B.MINGC MC,B.DANW DW,A.ZHUJ ZJ,A.QIUJ QJ,B.TIAOM TM,B.SPLEIX SPLX,( select LEIXMC from glass_spleix where LEIXBH=B.SPLEIX) SPLXMC,A.SHUL SL,A.DANJ DJ,A.ZHEK ZK,A.ZHEHDJ ZHDJ,A.ZHEHXJ ZHXJ,A.MOLING ML,A.HEJ XJ,(CASE A.ISZS WHEN 0 THEN '否' ELSE '是' END) ISZS,A.BEIZHU BZ FROM GLASS_XIAOSMX A,GLASS_SPXX B WHERE A.SHANGPBH=B.BIANH AND A.XIAOSDBH='" + XSDBH + "'").Tables[0]; List<auxItemModel> ListauxItemModel = new List<auxItemModel>(); for (int i = 0; i < dataitems.Rows.Count; i++) { auxItemModel auxItemModel = new PankuSoft.Model.auxItemModel(); auxItemModel.auxItemCode = SMBH; //dataitems.Rows[i]["SP"].ToString(); auxItemModel.auxItemName = dataitems.Rows[i]["MC"].ToString(); auxItemModel.auxItemRelatedCode = SMBH;//dataitems.Rows[i]["SPLX"].ToString(); ; auxItemModel.auxItemRelatedName = dataitems.Rows[i]["SPLXMC"].ToString(); auxItemModel.auxItemQuantity = dataitems.Rows[i]["SL"].ToString(); auxItemModel.auxItemUnit = dataitems.Rows[i]["DW"].ToString(); auxItemModel.auxItemStd = dataitems.Rows[i]["DJ"].ToString(); auxItemModel.auxItemAmount = dataitems.Rows[i]["XJ"].ToString(); auxItemModel.auxItemRemark = dataitems.Rows[i]["BZ"].ToString(); ListauxItemModel.Add(auxItemModel); } List<DZPJCREATEINVOICEITEMS> ListItem = new List<DZPJCREATEINVOICEITEMS>() ; DZPJCREATEINVOICEITEMS items = new DZPJCREATEINVOICEITEMS(); items.itemCode = SMBH; items.itemName = "视光门诊"; items.itemStd = Math.Round((Convert.ToDecimal(dt.Rows[0]["JINE"].ToString())/2),2).ToString(); items.itemUnit = "元"; items.itemAmount = dataitems.Rows.Count.ToString(); items.amount = dt.Rows[0]["JINE"].ToString(); items.note = dataitems.Rows[0]["BZ"].ToString(); items.auxItem = ListauxItemModel; ListItem.Add(items); DZPJCREATEINVOICE DZPJKP = new DZPJCREATEINVOICE() { reqSource = "2", placeCode = "", patType = "1", patNumber = SKDBM, email = "", phone = "", idcardNo = "", type = "1", payerPartyType = "1", payerPartyCode = "", payerPartyName = dt.Rows[0]["XINGM"].ToString(), payerAcct = "", payerOpBk = "", payMode = cmb_FKFS.Text == "支付宝" ? "4" : cmb_FKFS.Text == "微信" ? "4" : "1", recName = "", recOpBk = "", recAcct = "", totalAmount = dt.Rows[0]["JINE"].ToString(), repCreater = dt.Rows[0]["CAOZY"].ToString(), checker = dt.Rows[0]["CAOZY"].ToString(), mainExt = new object { }, items = ListItem }; DZPJRequestModel DZPJModel = new DZPJRequestModel() { method = DZPJ.createInvoice, content = JsonHelper.ObjectToJson(DZPJKP), operatorCode = Common.userBH, operatorName = Common.userMC, requestId = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + SKDBM, security = "" }; #endregion string PostUrl = ini.IniReadValue("DZFPURL", "ADDRESS"); var data = JsonHelper.JsonToObject<DZPJResponseModel>(HttpRespost.Post(DZPJModel, PostUrl)) as DZPJResponseModel; if (data.code == "0") { XtraMessageBox.Show("电子票据开具失败,请开先纸质票,然后联系管理员!", "提示"); if (DialogResult.OK==XtraMessageBox.Show("是否开纸质凭证","提示",MessageBoxButtons.OKCancel)) { if (cmb_FKFS.Text == "微信支付") { ReportClass.PrintShouKDXP(SKDBM); } if (cmb_FKFS.Text != "医保") { ReportClass.PrintShouKD(SKDBM); } } } else { var Result = JsonHelper.JsonToObject<DZPJCREATEINVOICERESULT>(data.data.ToString()); string strSql = $"INSERT INTO DZPJCREATEINVOICERESULT VALUES ('{Result.SERIALNUMBER}','{Result.EINVOICECODE }','{Result.EINVOICENUMBER}','{Result.RANDOMNUMBER}','{Result.ISSUEDATE}','{Result.ISSUETIME}','{Result.BILLPDFADDR}','{Result.BILLPDFURL}','{Result.PLATFORMCHECKURL}','{Result.FILE}','{Result.NOWSTOCK}')"; int res = DbHelperOra.ExecuteSql(strSql); ReportClass.PrintShouKDXPDZPJ(SKDBM); } }