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

时间:2017-03-14 14:51:28

标签: c# json

我有一些数据,当我检索单个数据条目时,反序列化很好,

  

无法反序列化当前的JSON对象(例如{“name”:“value”})   进入类型'System.Collections.Generic.List`1 [Purchaseorderline]'   因为该类型需要一个JSON数组(例如[1,2,3])来反序列化   正确。

这是检索Orders和OrderItems,我可以做一个订单(通过请求一个订单号)但是当我得到所有订单时,它会出错, 我已经执行了标准转换为Json对象以确保我的类是正确的,并且无法看到我应该作为数组应用到我的类中,只需将其放入类型为RootObject的List中。

这是我的代码:

      {
                // ... Read the string.
                var result1 = await content.ReadAsStringAsync();
                List<Rootobject> POOrders = new List<Rootobject>();
                Rootobject test = JsonConvert.DeserializeObject<Rootobject>(result1);
                POOrders.Add(test);
                POOrders = JsonConvert.DeserializeObject<List<Rootobject>>(result1);
             }

这是班级:

using Newtonsoft.Json;
using System;
using SFOrders;

public class Rootobject:Purchaseorderline
{
     [JsonProperty("PurchaseOrderID")]
    public string PurchaseOrderID { get; set; }
    [JsonProperty("PurchaseOrderGuid")]
    public string PurchaseOrderGuid { get; set; }
    [JsonProperty("User")]
    public User User { get; set; }
    public string PurchaseOrderReference { get; set; }
     [JsonProperty("DateRaised")]
    public DateTime DateRaised { get; set; }
    [JsonProperty("Supplier")]
    public Supplier Supplier { get; set; }
    [JsonProperty("Warehouse")]
    public Warehouse Warehouse { get; set; }
     [JsonProperty("DateDue")]
    public DateTime DateDue { get; set; }
     [JsonProperty("PurchaseOrderStatus")]
    public string PurchaseOrderStatus { get; set; }
    [JsonProperty("DeliveryCost")]
    public float DeliveryCost { get; set; }
    [JsonProperty("Subtotal")]
    public float Subtotal { get; set; }
    [JsonProperty("TotalVat")]
    public float TotalVat { get; set; }
    [JsonProperty("Total")]
    public float Total { get; set; }
    [JsonProperty("Currency")]
    public Currency Currency { get; set; }
    [JsonProperty("DateSent")]
    public DateTime DateSent { get; set; }
     [JsonProperty("PurchaseOrderLines")]
    public Purchaseorderline[] PurchaseOrderLines { get; set; }
}

public class User
{
    [JsonProperty("UserID")]
    public int UserID { get; set; }
     [JsonProperty("UserName")]
    public string UserName { get; set; }
}

public class Supplier
{
     [JsonProperty("SupplierID")]
    public int SupplierID { get; set; }
    [JsonProperty("Name")]
    public string Name { get; set; }
}

public class Warehouse
{

    [JsonProperty("WarehouseName")]
    public string WarehouseName { get; set; }
     [JsonProperty("WarehouseGuid")]
    public string WarehouseGuid { get; set; }
    [JsonProperty("WarehouseID")]
    public int WarehouseID { get; set; }
}

public class Currency
{
     [JsonProperty("CurrencyID")]
    public int CurrencyID { get; set; }
    [JsonProperty("CurrencyCode")]
    public string CurrencyCode { get; set; }
     [JsonProperty("CurrencyName")]
    public string CurrencyName { get; set; }
}

public class Purchaseorderline
{
    [JsonProperty("PurchaseOrderLineID")]
    public int PurchaseOrderLineID { get; set; }
    [JsonProperty("ProductSKU")]
    public string ProductSKU { get; set; }
    [JsonProperty("QtyOrdered")]
    public int QtyOrdered { get; set; }
     [JsonProperty("QtyReceived")]
    public int QtyReceived { get; set; }
    [JsonProperty("PurchaseOrderDetailStatus")]
    public string PurchaseOrderDetailsStatus { get; set; }
     [JsonProperty("SinglePrice")]
    public float SinglePrice { get; set; }
    [JsonProperty("LineVat")]
    public float LineVat { get; set; }
    [JsonProperty("LineTotal")]
    public float LineTotal { get; set; }
    [JsonProperty("DeliveryStatusID")]
    public int DeliveryStatusID { get; set; }
     [JsonProperty("QtyWrittenOff")]
    public float QtyWrittenOff { get; set; }
    [JsonProperty("CartonQty")]
    public int CartonQty { get; set; }
     [JsonProperty("AddedByUserID")]
    public int AddedByUserID { get; set; }
     [JsonProperty("TotalUnitsOrdered")]
    public int TotalUnitsOrdered { get; set; }

    [JsonProperty("TotalUnitsReceived")]
    public int TotalUnitsReceived { get; set; }
     [JsonProperty("TotalUnitsWrittenOff")]
    public int TotalUnitsWrittenOff { get; set; }
    [JsonProperty("LineNotes")]
    public string LineNotes { get; set; }



     [JsonProperty("TotalItems")]
    public int TotalItems { get; set; }
     [JsonProperty("PagingInfo")]
    public object PagingInfo { get; set; }
     [JsonProperty("TotalPages")]
    public int TotalPages { get; set; }
     [JsonProperty("Data")]
    public SFOrders.SFOrderData.Datum[] Data { get; set; }


}

这是原始数据:

{
   "TotalItems":2,
   "PagingInfo":{
      "PageSize":100,
      "Page":1
   },
   "TotalPages":1,
   "Data":[
      {
         "PurchaseOrderID":21179,
         "PurchaseOrderGuid":"89bb1056-4d26-42a2-bf9c-25962637b42e",
         "User":{
            "UserID":10169,
            "UserName":"Viv Williams"
         },
         "PurchaseOrderReference":"1-test-1",
         "DateRaised":"2017-03-03T00:00:00",
         "Supplier":{
            "SupplierID":1307,
            "Name":"99A  Avery Warehouse"
         },
         "Warehouse":{
            "WarehouseName":"Charlies Direct",
            "WarehouseGuid":"00000000-0000-0000-0000-000000000000",
            "WarehouseID":122
         },
         "DateDue":"2017-03-04T00:00:00",
         "PurchaseOrderStatus":"SentToSupplier",
         "DeliveryCost":0.0000,
         "Subtotal":0.0000,
         "TotalVat":0.0000,
         "Total":0.0000,
         "Currency":{
            "CurrencyID":4,
            "CurrencyCode":"GBP",
            "CurrencyName":"British Pound"
         },
         "DateSent":"2017-03-03T12:13:59.05",
         "PurchaseOrderLines":[
            {
               "PurchaseOrderLineID":184694,
               "ProductSKU":"065233",
               "QtyOrdered":3,
               "QtyReceived":0,
               "PurchaseOrderDetailsStatus":"AwaitingProduct",
               "SinglePrice":0.0000,
               "LineVat":0.0000,
               "LineTotal":0.0000,
               "DeliveryStatusID":1,
               "QtyWrittenOff":0.0,
               "CartonQty":1,
               "AddedByUserID":10169,
               "TotalUnitsOrdered":3,
               "TotalUnitsReceived":0,
               "TotalUnitsWrittenOff":0,
               "LineNotes":""
            },
            {
               "PurchaseOrderLineID":184695,
               "ProductSKU":"039586",
               "QtyOrdered":10,
               "QtyReceived":0,
               "PurchaseOrderDetailsStatus":"AwaitingProduct",
               "SinglePrice":0.0000,
               "LineVat":0.0000,
               "LineTotal":0.0000,
               "DeliveryStatusID":1,
               "QtyWrittenOff":0.0,
               "CartonQty":1,
               "AddedByUserID":10169,
               "TotalUnitsOrdered":10,
               "TotalUnitsReceived":0,
               "TotalUnitsWrittenOff":0,
               "LineNotes":""
            },
            {
               "PurchaseOrderLineID":184696,
               "ProductSKU":"009493",
               "QtyOrdered":2,
               "QtyReceived":0,
               "PurchaseOrderDetailsStatus":"AwaitingProduct",
               "SinglePrice":0.0000,
               "LineVat":0.0000,
               "LineTotal":0.0000,
               "DeliveryStatusID":1,
               "QtyWrittenOff":0.0,
               "CartonQty":1,
               "AddedByUserID":10169,
               "TotalUnitsOrdered":2,
               "TotalUnitsReceived":0,
               "TotalUnitsWrittenOff":0,
               "LineNotes":""
            },
            {
               "PurchaseOrderLineID":184697,
               "ProductSKU":"002836",
               "QtyOrdered":5,
               "QtyReceived":0,
               "PurchaseOrderDetailsStatus":"AwaitingProduct",
               "SinglePrice":0.0000,
               "LineVat":0.0000,
               "LineTotal":0.0000,
               "DeliveryStatusID":1,
               "QtyWrittenOff":0.0,
               "CartonQty":1,
               "AddedByUserID":10169,
               "TotalUnitsOrdered":5,
               "TotalUnitsReceived":0,
               "TotalUnitsWrittenOff":0,
               "LineNotes":""
            }
         ]
      },
      {
         "PurchaseOrderID":21180,
         "PurchaseOrderGuid":"854c1c5a-f63b-490e-99dc-119ed298988a",
         "User":{
            "UserID":10169,
            "UserName":"Viv Williams"
         },
         "PurchaseOrderReference":"2 add Tues",
         "DateRaised":"2017-03-03T00:00:00",
         "Supplier":{
            "SupplierID":1298,
            "Name":"99D  Dyffryn Warehouse"
         },
         "Warehouse":{
            "WarehouseName":"Charlies Direct",
            "WarehouseGuid":"00000000-0000-0000-0000-000000000000",
            "WarehouseID":122
         },
         "DateDue":"2017-03-04T00:00:00",
         "PurchaseOrderStatus":"SentToSupplier",
         "DeliveryCost":0.0000,
         "Subtotal":0.0000,
         "TotalVat":0.0000,
         "Total":0.0000,
         "Currency":{
            "CurrencyID":4,
            "CurrencyCode":"GBP",
            "CurrencyName":"British Pound"
         },
         "DateSent":"2017-03-03T12:13:48.19",
         "PurchaseOrderLines":[
            {
               "PurchaseOrderLineID":184698,
               "ProductSKU":"003010",
               "QtyOrdered":5,
               "QtyReceived":0,
               "PurchaseOrderDetailsStatus":"AwaitingProduct",
               "SinglePrice":0.0000,
               "LineVat":0.0000,
               "LineTotal":0.0000,
               "DeliveryStatusID":1,
               "QtyWrittenOff":0.0,
               "CartonQty":1,
               "AddedByUserID":10169,
               "TotalUnitsOrdered":5,
               "TotalUnitsReceived":0,
               "TotalUnitsWrittenOff":0,
               "LineNotes":""
            },
            {
               "PurchaseOrderLineID":184699,
               "ProductSKU":"094508",
               "QtyOrdered":12,
               "QtyReceived":0,
               "PurchaseOrderDetailsStatus":"AwaitingProduct",
               "SinglePrice":0.0000,
               "LineVat":0.0000,
               "LineTotal":0.0000,
               "DeliveryStatusID":1,
               "QtyWrittenOff":0.0,
               "CartonQty":1,
               "AddedByUserID":10169,
               "TotalUnitsOrdered":12,
               "TotalUnitsReceived":0,
               "TotalUnitsWrittenOff":0,
               "LineNotes":""
            },
            {
               "PurchaseOrderLineID":184700,
               "ProductSKU":"003820",
               "QtyOrdered":20,
               "QtyReceived":0,
               "PurchaseOrderDetailsStatus":"AwaitingProduct",
               "SinglePrice":0.0000,
               "LineVat":0.0000,
               "LineTotal":0.0000,
               "DeliveryStatusID":1,
               "QtyWrittenOff":0.0,
               "CartonQty":1,
               "AddedByUserID":10169,
               "TotalUnitsOrdered":20,
               "TotalUnitsReceived":0,
               "TotalUnitsWrittenOff":0,
               "LineNotes":""
            },
            {
               "PurchaseOrderLineID":184701,
               "ProductSKU":"003823",
               "QtyOrdered":4,
               "QtyReceived":0,
               "PurchaseOrderDetailsStatus":"AwaitingProduct",
               "SinglePrice":0.0000,
               "LineVat":0.0000,
               "LineTotal":0.0000,
               "DeliveryStatusID":1,
               "QtyWrittenOff":0.0,
               "CartonQty":1,
               "AddedByUserID":10169,
               "TotalUnitsOrdered":4,
               "TotalUnitsReceived":0,
               "TotalUnitsWrittenOff":0,
               "LineNotes":""
            }
         ]
      }
   ]
}

3 个答案:

答案 0 :(得分:0)

看来您的JSON格式不正确。
Data元素应该是一个数组而不是一个对象。

以下内容应该:

{
   "TotalItems":2,
   "PagingInfo":{
      "PageSize":100,
      "Page":1
   },
   "TotalPages":1,
    "Data": [
        {
            "PurchaseOrderID": 21179,
            "PurchaseOrderGuid": "89bb1056-4d26-42a2-bf9c-25962637b42e",
            "User": {
                "UserID": 10169,
                "UserName": "Viv Williams"
            },
            "PurchaseOrderReference": "1-test-1",
            "DateRaised": "2017-03-03T00:00:00",
            "Supplier": {
                "SupplierID": 1307,
                "Name": "99A Avery Warehouse"
            },
            "Warehouse": {
                "WarehouseName": "Charlies Direct",
                "WarehouseGuid": "00000000-0000-0000-0000-000000000000",
                "WarehouseID": 122
            },
            "DateDue": "2017-03-04T00:00:00",
            "PurchaseOrderStatus": "SentToSupplier",
            "DeliveryCost": 0.0000,
            "Subtotal": 0.0000,
            "TotalVat": 0.0000,
            "Total": 0.0000,
            "Currency": {
                "CurrencyID": 4,
                "CurrencyCode": "GBP",
                "CurrencyName": "British Pound"
            },
            "DateSent": "2017-03-03T12:13:59.05",
            "PurchaseOrderLines": [
                {
                    "PurchaseOrderLineID": 184694,
                    "ProductSKU": "065233",
                    "QtyOrdered": 3,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 3,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                },
                {
                    "PurchaseOrderLineID": 184695,
                    "ProductSKU": "039586",
                    "QtyOrdered": 10,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 10,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                },
                {
                    "PurchaseOrderLineID": 184696,
                    "ProductSKU": "009493",
                    "QtyOrdered": 2,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 2,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                },
                {
                    "PurchaseOrderLineID": 184697,
                    "ProductSKU": "002836",
                    "QtyOrdered": 5,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 5,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                }
            ]
        },
        {
            "PurchaseOrderID": 21180,
            "PurchaseOrderGuid": "854c1c5a-f63b-490e-99dc-119ed298988a",
            "User": {
                "UserID": 10169,
                "UserName": "Viv Williams"
            },
            "PurchaseOrderReference": "2 add Tues",
            "DateRaised": "2017-03-03T00:00:00",
            "Supplier": {
                "SupplierID": 1298,
                "Name": "99D Dyffryn Warehouse"
            },
            "Warehouse": {
                "WarehouseName": "Charlies Direct",
                "WarehouseGuid": "00000000-0000-0000-0000-000000000000",
                "WarehouseID": 122
            },
            "DateDue": "2017-03-04T00:00:00",
            "PurchaseOrderStatus": "SentToSupplier",
            "DeliveryCost": 0.0000,
            "Subtotal": 0.0000,
            "TotalVat": 0.0000,
            "Total": 0.0000,
            "Currency": {
                "CurrencyID": 4,
                "CurrencyCode": "GBP",
                "CurrencyName": "British Pound"
            },
            "DateSent": "2017-03-03T12:13:48.19",
            "PurchaseOrderLines": [
                {
                    "PurchaseOrderLineID": 184698,
                    "ProductSKU": "003010",
                    "QtyOrdered": 5,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 5,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                },
                {
                    "PurchaseOrderLineID": 184699,
                    "ProductSKU": "094508",
                    "QtyOrdered": 12,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 12,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                },
                {
                    "PurchaseOrderLineID": 184700,
                    "ProductSKU": "003820",
                    "QtyOrdered": 20,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 20,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                },
                {
                    "PurchaseOrderLineID": 184701,
                    "ProductSKU": "003823",
                    "QtyOrdered": 4,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 4,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                }
            ]
        }
    ]
}

如果Data元素不应该是数组,那么在这种情况下,您的JSON包含额外的值,以下内容应该是您要查找的内容:

{
   "TotalItems":2,
   "PagingInfo":{
      "PageSize":100,
      "Page":1
   },
   "TotalPages":1,
    "Data": 
        {
            "PurchaseOrderID": 21179,
            "PurchaseOrderGuid": "89bb1056-4d26-42a2-bf9c-25962637b42e",
            "User": {
                "UserID": 10169,
                "UserName": "Viv Williams"
            },
            "PurchaseOrderReference": "1-test-1",
            "DateRaised": "2017-03-03T00:00:00",
            "Supplier": {
                "SupplierID": 1307,
                "Name": "99A Avery Warehouse"
            },
            "Warehouse": {
                "WarehouseName": "Charlies Direct",
                "WarehouseGuid": "00000000-0000-0000-0000-000000000000",
                "WarehouseID": 122
            },
            "DateDue": "2017-03-04T00:00:00",
            "PurchaseOrderStatus": "SentToSupplier",
            "DeliveryCost": 0.0000,
            "Subtotal": 0.0000,
            "TotalVat": 0.0000,
            "Total": 0.0000,
            "Currency": {
                "CurrencyID": 4,
                "CurrencyCode": "GBP",
                "CurrencyName": "British Pound"
            },
            "DateSent": "2017-03-03T12:13:59.05",
            "PurchaseOrderLines": [
                {
                    "PurchaseOrderLineID": 184694,
                    "ProductSKU": "065233",
                    "QtyOrdered": 3,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 3,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                },
                {
                    "PurchaseOrderLineID": 184695,
                    "ProductSKU": "039586",
                    "QtyOrdered": 10,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 10,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                },
                {
                    "PurchaseOrderLineID": 184696,
                    "ProductSKU": "009493",
                    "QtyOrdered": 2,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 2,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                },
                {
                    "PurchaseOrderLineID": 184697,
                    "ProductSKU": "002836",
                    "QtyOrdered": 5,
                    "QtyReceived": 0,
                    "PurchaseOrderDetailsStatus": "AwaitingProduct",
                    "SinglePrice": 0.0000,
                    "LineVat": 0.0000,
                    "LineTotal": 0.0000,
                    "DeliveryStatusID": 1,
                    "QtyWrittenOff": 0.0,
                    "CartonQty": 1,
                    "AddedByUserID": 10169,
                    "TotalUnitsOrdered": 5,
                    "TotalUnitsReceived": 0,
                    "TotalUnitsWrittenOff": 0,
                    "LineNotes": ""
                }
            ]
        }

}

答案 1 :(得分:0)

您得到的错误是因为您尝试将JSON字符串反序列化为Rootobjects而不是Rootobject本身。

还可以通过复制JSON字符串(完整的字符串)并通过编辑&gt;粘贴它来生成类。选择性粘贴&gt;将JSON粘贴为Visual Studio中的类。

这是Rootobject:

public class Rootobject
{
    public int TotalItems { get; set; }
    public Paginginfo PagingInfo { get; set; }
    public int TotalPages { get; set; }
    public Datum[] Data { get; set; }
}

public class Paginginfo
{
    public int PageSize { get; set; }
    public int Page { get; set; }
}

public class Datum
{
    public int PurchaseOrderID { get; set; }
    public string PurchaseOrderGuid { get; set; }
    public User User { get; set; }
    public string PurchaseOrderReference { get; set; }
    public DateTime DateRaised { get; set; }
    public Supplier Supplier { get; set; }
    public Warehouse Warehouse { get; set; }
    public DateTime DateDue { get; set; }
    public string PurchaseOrderStatus { get; set; }
    public float DeliveryCost { get; set; }
    public float Subtotal { get; set; }
    public float TotalVat { get; set; }
    public float Total { get; set; }
    public Currency Currency { get; set; }
    public DateTime DateSent { get; set; }
    public Purchaseorderline[] PurchaseOrderLines { get; set; }
}

public class User
{
    public int UserID { get; set; }
    public string UserName { get; set; }
}

public class Supplier
{
    public int SupplierID { get; set; }
    public string Name { get; set; }
}

public class Warehouse
{
    public string WarehouseName { get; set; }
    public string WarehouseGuid { get; set; }
    public int WarehouseID { get; set; }
}

public class Currency
{
    public int CurrencyID { get; set; }
    public string CurrencyCode { get; set; }
    public string CurrencyName { get; set; }
}

public class Purchaseorderline
{
    public int PurchaseOrderLineID { get; set; }
    public string ProductSKU { get; set; }
    public int QtyOrdered { get; set; }
    public int QtyReceived { get; set; }
    public string PurchaseOrderDetailsStatus { get; set; }
    public float SinglePrice { get; set; }
    public float LineVat { get; set; }
    public float LineTotal { get; set; }
    public int DeliveryStatusID { get; set; }
    public float QtyWrittenOff { get; set; }
    public int CartonQty { get; set; }
    public int AddedByUserID { get; set; }
    public int TotalUnitsOrdered { get; set; }
    public int TotalUnitsReceived { get; set; }
    public int TotalUnitsWrittenOff { get; set; }
    public string LineNotes { get; set; }
}

但是如果你的JSON字符串包含一个Rootobjects数组,就像这样:

[{RawData1},{RawData2},{RawData3}]

然后你可以按如下方式反序列化它:

var test = JsonConvert.DeserializeObject<List<Rootobject>>(json);

答案 2 :(得分:0)

睡在上面......它类似于你的答案是Mahdi thankyou, 我需要对Raw进行反序列化,然后通过反序列化的对象将其添加到列表中,   Rootobject test = JsonConvert.DeserializeObject&lt; Rootobject&gt;(result1);                foreach(test.Data中的Rootobject项)                {                    POOrders.Add(项目);                }