json字符串转C#对象
说明:如果列表增加了了一个字段,名字为id,类型为int,它原来的List<Class>中的Class增加了一个id字段。C#序列化转换不会报错,但是每个List会给id默认为0。
=================================================================
#region----聚合数据,银行卡类型及真伪查询,返回实体,2017-12-27 09:11新增---- /// <summary> /// 聚合数据,银行卡类型及真伪查询,返回实体 /// </summary> public class JuheBankCardResponse { //请求错误返回示例: //{"resultcode":"101","reason":"错误的请求KEY!","result":null,"error_code":10001} //{"reason":"银行卡号错误","result":null,"error_code":229902} //返回成功示例: //{"reason":"查询成功 ","result":{"bank":"中国民生银行","cardtype":"借记卡","nature":"借记卡普卡","kefu":"95568","bankcard":"621691031020361","logo":"http:\/\/apiserver.qiniudn.com\/minsheng.png","info":""},"error_code":0} /// <summary> /// 返回说明 /// </summary> public string reason { get; set; } /// <summary> /// 返回结果参数 /// </summary> public List<JuHeBankCardResult> result { get; set; } /// <summary> /// 返回码 /// </summary> public int error_code { get; set; } }
/// <summary> /// 返回结果参数实体 /// </summary> public class JuHeBankCardResult { /// <summary> /// 银行归属 ///例如:中国民生银行 /// </summary> public string bank { get; set; } /// <summary> /// 卡类型 ///例如:借记卡 /// </summary> public string cardtype { get; set; } /// <summary> /// 银行卡种类 ///例如:借记卡普卡 /// </summary> public string nature { get; set; } /// <summary> /// 客服电话 /// </summary> public string kefu { get; set; } /// <summary> /// 银行卡号 /// </summary> public string bankcard { get; set; } /// <summary> /// 银行Logo ///例如:http:\/\/apiserver.qiniudn.com\/minsheng.png /// </summary> public string logo { get; set; } /// <summary> /// 银行卡归属信息(暂支持农业) /// </summary> public string info { get; set; } } #endregion
string resultJson = " { \"reason\":\"查询成功 \",\"result\":{ \"bank\":\"中国民生银行\",\"cardtype\":\"借记卡\",\"nature\":\"借记卡普卡\",\"kefu\":\"95568\",\"bankcard\":\"621691031020361\",\"logo\":\"http://apiserver.qiniudn.com/minsheng.png\",\"info\":\"\"},\"error_code\":0} "; try { JuheBankCardResponse bankCardResponse = new JuheBankCardResponse(); List<JuHeBankCardResult> bankCardResult = new List<JuHeBankCardResult>(); bankCardResponse.result = bankCardResult; Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(resultJson); foreach (var item in dic) { if (item.Key == "reason") { bankCardResponse.reason = item.Value.ToString(); } if (item.Key == "result") { JuHeBankCardResult bankCardResultDic = new JuHeBankCardResult(); bankCardResultDic = JsonConvert.DeserializeObject<JuHeBankCardResult>(item.Value.ToString()); bankCardResult.Add(bankCardResultDic); } if (item.Key == "error_code") { bankCardResponse.error_code = Convert.ToInt32(item.Value); } } } catch (Exception ex) { //记录resultJson throw ex; }
=======================================================================
#region----准备提交预约页面,实体赋值---- PostMbAppointResult postMbAppointEntity = new PostMbAppointResult(); try { Appointment appointmentEntity = _appointAboutStoresServices.GetAppointmentEntityList(apID); if (appointmentEntity == null) { BaseJsonResult.PrintJson("0", "[DoPostAppointInfoReady()]没有查询到该活动相关数据"); return; } postMbAppointEntity.Title = appointmentEntity.Title; postMbAppointEntity.PicUrl = appointmentEntity.PicUrl; postMbAppointEntity.IsValPhone = appointmentEntity.IsValPhone;//是否需要验证手机 #region----拆解信息填写界面---- List<AppointGetWriteInfoEntity> appointGetWriteInfoEntity = null; if (!RegExpHelper.IsNull(appointmentEntity.CusInputeInfo)) { //拆解预约信息填写界面的实体 AppointSetWriteInfoResult jsonInfoResultObject = JsonConvert.DeserializeObject<AppointSetWriteInfoResult>(appointmentEntity.CusInputeInfo); List<AppointSetWriteInfoEntity> writeInfoEntityList = new List<AppointSetWriteInfoEntity>(); foreach (var item in jsonInfoResultObject.appointSetWriteInfoEntity) { writeInfoEntityList.Add(item); } appointGetWriteInfoEntity = new List<AppointGetWriteInfoEntity>(); foreach (var itemSet in writeInfoEntityList) { AppointGetWriteInfoEntity getWriteInfoEntity = new AppointGetWriteInfoEntity() { Filed = itemSet.Filed, Tips = itemSet.Tips, ValType = itemSet.ValType, IsNeed = itemSet.IsNeed, Sort = itemSet.Sort }; appointGetWriteInfoEntity.Add(getWriteInfoEntity); } } #endregion----拆解信息填写界面---- postMbAppointEntity.appointGetWriteInfoEntity = appointGetWriteInfoEntity; postMbAppointEntity.mbIntervalList = mbIntervalList; } catch (Exception ex) { Log4NetHelper.Log(Utility.Enums.LogTypeEnum.ServicesLog, Utility.Enums.LogLevelEnum.Error, currentPageUrlLog + "[DoPostAppointInfoReady()]准备提交预约页面获取数据报错", ex); BaseJsonResult.PrintJson("0", "[DoPostAppointInfoReady()]准备提交预约页面获取数据报错"); return; } #endregion----准备提交预约页面,实体赋值----