簡體   English   中英

無法反序列化當前的JSON對象

[英]Cannot deserialize the current JSON object

無法將當前JSON對象(例如{"name":"value"} )反序列化為類型'System.Collections.Generic.List 1 [NIPRA.Models.OrderHeaderResult]', because the type requires a JSON數組(例如[1] ,2,3])`正確地序列化。

要修復此錯誤,請將JSON更改為JSON array (eg [1,2,3])或更改De-Serialized類型,使其成為普通的.NET類型(例如,不是像integer這樣的基本類型,而不是集合類似於數組或List的類型,可以從JSON對象中進行反序列化。 還可以將JsonObjectAttribute添加到類型中以強制它從JSON對象中進行反序列化。

路徑'訂單',第1行,第10位。

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}

我的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模型:

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

    }

請幫助以及如何在剃刀視圖上顯示這個?

你的JSON肯定不像一個集合。
您有一個JSON對象,在OrderHeaderResult類中已正確描述。

嘗試改變

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

JsonConvert.DeserializeObject<OrderHeaderResult>(orderData);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM