/// <summary> /// 需求单列表 /// </summary> /// <param name="model"></param> /// <returns></returns> public ResponseEntity List(InquiryOrderListRequestModel model) { ResponseEntity response = 调用接口返回的数据 if (response.Header.Success && response.Body != null) { //定义匿名类,作为反序列的模板 var data = new { PageCount = 0, TotalCount = 0, InquiryOrderModels = new[] { new { Id = 0, Departure = "", Status = 0, CreateTime = DateTime.Now, DestinationList = "" } } }; var inquiryOrderListResponseModel = JsonConvert.DeserializeAnonymousType(response.Body.ToString(), data); //老的方式,有的没的都返回了 //var inquiryOrderListResponseModel = JsonConvert.DeserializeObject<InquiryOrderListResponseModel>(response.Body.ToString()); response.Body = inquiryOrderListResponseModel; } return response; }
假设有个实体有几十个字段或属性,我们提供给用户的接口只用到几个,而我们又不想新定义一个类。那么这个时候我们就可以使用自定义匿名类的方式来处理这种需求。当然需要基于Newtonsoft。