如何将 JSON 对象(从 Web API 返回)映射到视图模型。[我正在使用 automapper 将我的 c# 业务对象映射到视图模型。我正在寻找类似于将 JSON 映射到视图模型的东西]
我收到以下错误:“无法将当前 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
}
}
内部异常:
{"无法将当前 JSON 对象 (例如 {\"name\":\"value\"}) 反序列化为类型 'System.Collections.Generic.IEnumerable`1[CLAW.BLL.MLAReports.Models.CMAReport]',因为type 需要 JSON 数组(例如 [1,2,3])才能正确反序列化。\r\n要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化的类型,以便它是可以从 JSON 对象反序列化的普通 .NET 类型(例如,不是整数之类的原始类型,不是数组或列表之类的集合类型)。也可以将 JsonObjectAttribute 添加到该类型以强制其反序列化一个 JSON 对象。\r\n路径 'CmaList',第 1 行,位置 11。"}