如何将JSON对象(从Web API返回)映射到View Model。 [我正在使用automapper将我的c#业务对象映射到View Model。我正在寻找类似于将JSON映射到View Model的东西
我收到以下错误:"无法将当前JSON对象反序列化为类型' System.Collections.Generic.IEnumerable`1 [CLAW.BLL.MLAReports.Models.CMAReport]'因为类型需要一个JSON数组"。
这是我在Webapi中的控制器的签名。
namespace ReportsAPI.Controllers
{
public class ReportsController : ApiController
{
[HttpPost]
public CMAReportVM Reports([FromBody] StatsCriteria criteria)
{
var cmaReport = Service3.GetCMAReport(criteria.Masnums);
//Create Map to enable mapping business object to View Model
Mapper.CreateMap<CMAReport, CMAReportVM>();
// Maps model to VM model class
var cmaVM = Mapper.Map<CMAReport, CMAReportVM>(cmaReport);
reutn cmaVM;
}
}
}
这是客户端控制器的签名:
namespace MASReports.Controllers
{
public ActionResult Reports(StatsCriteria criteria)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsJsonAsync("http://localhost:52765/api/reports", criteria.Masnums.ToString()).Result;
// The below line I get error.
var represlt= response.Content.ReadAsAsync<IEnumerable<CMAReportVM>>().Result;
return View("CMAReportVM", represlt);
}
}
Here's the signature for the model :
namespace MLAReports.Models
{
public class CMAReportVM:ReportsVM
{
public CMAReportVM() {}
public List<Cma_Item> CmaList { get; set; }
public string TimeStampString { get; set; }
public Dictionary<string, string> CriteriaNameValue { get; set; }
}
}
namespace CLAW.BLL.MLAReports.Models
{
public class Cma_Item : IStatsGridable<Cma_Fields>
{
public Cma_Item() {}
#region Properties and Fields
public string PropTypeName { get; set; }
public string StatusName { get; set; }
public string LowP { get; set; }
public string LowXStLP { get; set; }
public string HighP { get; set; }
public decimal? AvgLPFnl { get; set; }
public decimal? AvgSPFnl { get; set; }
#endregion
#region IStatsGrid Properties
public List<string> Headers
{
get
{ return _headers; }
set
{ _headers = value; }
}
private IEnumerable<Cma_Fields> _rowModels;
public IEnumerable<Cma_Fields> RowModels
{
get
{ return _rowModels;}
set
{ _rowModels = value;}
}
private List<string> _headers;
private string _id;
#endregion
}
}
namespace CLAW.BLL.MLaReports.Models
{
public interface IStatsGridable<T>
{
List<string> Headers { get; set; }
string Id { get; set; }
IEnumerable<T> RowModels { get; set; }
}
}
namespace CLAW.BLL.MLAReports.Models
{
public class Cma_Fields
{
public Cma_Fields() { }
#region Properties and Fields
public string Mlanum { get; set; }
public string Address { get; set; }
public string BR { get; set; }
public string BA { get; set; }
public string SF { get; set; }
public string TRId
{
get
{ return _trid; }
set
{_trid = value; }
}
#endregion
}
}
InnerException:
{&#34;无法将当前JSON对象(例如{\&#34; name \&#34;:\&#34; value \&#34;})反序列化为类型&System;系统。 Collections.Generic.IEnumerable`1 [CLAW.BLL.MLAReports.Models.CMAReport]&#39;因为类型需要一个JSON数组(例如[1,2,3])才能正确反序列化。\ r \ n要解决此错误,要么将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,以便它是一个普通的.NET类型(例如,不是像整数的基本类型,不是像数组或列表那样的集合类型),可以从JSON对象反序列化。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。\ r \ n路由&#39; CmaList&#39;,第1行,第11位。&#34;}
答案 0 :(得分:1)
您的报告控制器签名正在返回单个项目
public CMAReportVM Reports([FromBody] StatsCriteria criteria)
但你试图把它读作IEnumerable
var represlt= response.Content.ReadAsAsync<IEnumerable<CMAReportVM>>().Result;