简体   繁体   English

无法反序列化当前的JSON对象

[英]Cannot deserialize the current JSON object

Cannot De-Serialize the current JSON object (eg {"name":"value"} ) into type 'System.Collections.Generic.List 1[NIPRA.Models.OrderHeaderResult]' because the type requires a JSON array (eg [1,2,3])` to De-Serialize correctly. 无法将当前JSON对象(例如{"name":"value"} )反序列化为类型'System.Collections.Generic.List 1 [NIPRA.Models.OrderHeaderResult]', because the type requires a JSON数组(例如[1] ,2,3])`正确地序列化。

To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the De-Serialized type so that it is a normal .NET type (eg not a primitive type like integer , not a collection type like an array or List) that can be De-Serialized from a JSON object. 要修复此错误,请将JSON更改为JSON array (eg [1,2,3])或更改De-Serialized类型,使其成为普通的.NET类型(例如,不是像integer这样的基本类型,而不是集合类似于数组或List的类型,可以从JSON对象中进行反序列化。 JsonObjectAttribute can also be added to the type to force it to De-Serialize from a JSON object. 还可以将JsonObjectAttribute添加到类型中以强制它从JSON对象中进行反序列化。

Path 'Orders', line 1, position 10. 路径'订单',第1行,第10位。

Json data: 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}

My viewmodel: 我的viewmodel:

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 model: 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;
    }

controller: 控制器:

    [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);

    }

please help and how to display this on razor view ? 请帮助以及如何在剃刀视图上显示这个?

Your JSON definitely does not look like a collection. 你的JSON肯定不像一个集合。
You have a single JSON object, which is properly described in OrderHeaderResult class. 您有一个JSON对象,在OrderHeaderResult类中已正确描述。

Try changing 尝试改变

JsonConvert.DeserializeObject<List<OrderHeaderResult>>(orderData);

to

JsonConvert.DeserializeObject<OrderHeaderResult>(orderData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM