无法将当前JSON
对象(例如{"name":"value"}
)反序列化为类型'System.Collections.Generic.List
1 [NIPRA.Models.OrderHeaderResult]'because the type requires a
JSON数组(例如[1] ,2,3])`正确地序列化。
要修复此错误,请将JSON
更改为JSON array (e.g. [1,2,3])
或更改De-Serialized类型,使其成为正常的.NET类型(例如,不是像integer
这样的原始类型,不是可以从JSON对象反序列化的集合类型,如数组或List)。 JsonObjectAttribute也可以添加到类型中,以强制它从JSON对象中进行反序列化。
Json数据:
{"Orders":
[{"Id":397908,
"BuyerId":1831,
"DateCreated":"2016-02-16T10:58:55",
"DateUpdated":"2016-02-16T10:58:55",
"DeliveryDate":"2015-01-20T00:00:00",
"UploadDate":"2016-02-16T10:58:55",
"InvoiceToName":"Lancet Laboratories (Pty) Ltd",
"OrderDate":"2016-02-16T08:58:55",
"OrderNumber":"PTYPO000006",
"OrderStatusId":1,
"OrderTotal":42050.3500,
"OrderTypeId":2,
"PartnerId":"V0002066 ",
"SupplierAccountName":"Bio-Rad Laboratories (Pty) Ltd",
"SupplierId":1696,
"ShipToName":"Rich Corn",
"SystemOrderStatusId":1,
"TermsOfPayment":"30DS",
"Supplier":null,
"Buyer":"Lancet Laboratories",
"BuyerName":"juri.vdv",
"BuyerAccountNumber":"",
"OrderStatus":"Requested",
"OrderType":"PURCHASE",
"HasComments":"No",
"TotalExcludingVAT":36886.2700,
"VATAmount":5164.0800,"AmendedBy":"System",
"FileName":""}],
"TotalPages":2}
我的观点模型:
public partial class OrderHeaderView
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("buyerId")]
public int BuyerId { get; set; }
[JsonProperty("dateCreated")]
public System.DateTime DateCreated { get; set; }
[JsonProperty("dateUpdated")]
public System.DateTime DateUpdated { get; set; }
[JsonProperty("deliveryDate")]
public Nullable<System.DateTime> DeliveryDate { get; set; }
[JsonProperty("uploadDate")]
public Nullable<System.DateTime> UploadDate { get; set; }
[JsonProperty("invoiceToName")]
public string InvoiceToName { get; set; }
[JsonProperty("orderDate")]
public Nullable<System.DateTime> OrderDate { get; set; }
[JsonProperty("orderNumber")]
public string OrderNumber { get; set; }
[JsonProperty("orderStatusId")]
public int OrderStatusId { get; set; }
[JsonProperty("orderTotal")]
public decimal OrderTotal { get; set; }
[JsonProperty("orderTypeId")]
public int OrderTypeId { get; set; }
[JsonProperty("partnerId")]
public string PartnerId { get; set; }
[JsonProperty("supplierAccountName")]
public string SupplierAccountName { get; set; }
[JsonProperty("supplierId")]
public int SupplierId { get; set; }
[JsonProperty("shipToName")]
public string ShipToName { get; set; }
[JsonProperty("systemOrderStatusId")]
public Nullable<int> SystemOrderStatusId { get; set; }
[JsonProperty("termsOfPayment")]
public string TermsOfPayment { get; set; }
[JsonProperty("supplier")]
public string Supplier { get; set; }
[JsonProperty("buyer")]
public string Buyer { get; set; }
[JsonProperty("buyerName")]
public string BuyerName { get; set; }
[JsonProperty("buyerAccountNumber")]
public string BuyerAccountNumber { get; set; }
[JsonProperty("orderStatu")]
public string OrderStatus { get; set; }
[JsonProperty("orderType")]
public string OrderType { get; set; }
[JsonProperty("hasComments")]
public string HasComments { get; set; }
[JsonProperty("totalExcludingVAT")]
public decimal TotalExcludingVAT { get; set; }
[JsonProperty("vatAmount")]
public decimal VATAmount { get; set; }
[JsonProperty("amendedBy")]
public string AmendedBy { get; set; }
[JsonProperty("fileName")]
public string FileName { get; set; }
[JsonProperty("hasDelivery")]
public bool HasDelivery { get; set; }
}
RootObject模型:
public class OrderHeaderResult
{
public int TotalPages { get; set; }
public OrderHeaderView[] Orders { get; set; }
}
public async Task<List<OrderHeaderResult>> GetOrders(int statusId, int page, string keyword, int year)
{
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", UserToken.AccessToken);
var result = await _client.GetAsync($"api/invoicing/getorders?statusId={statusId}&page={page}&keyword={keyword}&year={year}");
if (result.IsSuccessStatusCode)
{
var orderData = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<OrderHeaderResult>>(orderData);
}
else
return null;
}
控制器:
[HttpGet]
public async Task<ActionResult> Index(int? id, int?page, string keyword, int?year)
{
_orderService.UserToken = base.Token;
var orders = await _orderService.GetOrders(id??1,page??2,keyword,year??DateTime.Now.Year);
return View(orders);
}
请帮助以及如何在剃刀视图中显示它?
答案 0 :(得分:0)
你的JSON绝对不像一个集合
您有一个JSON对象,在OrderHeaderResult
类中已正确描述。
尝试更改
JsonConvert.DeserializeObject<List<OrderHeaderResult>>(orderData);
到
JsonConvert.DeserializeObject<OrderHeaderResult>(orderData);