将多个json反序列化为对象c#

Ped*_*pes 3 c# serialization json

我正在尝试从具有多个对象的 API 调用中反序列化 json 字符串,但没有取得太大成功。

JSON:

@{
    "purchaseOrders": [
        {
            "supplierId": "500",
            "currencyCode": "EUR",
            "companyId": "LALA",
            "companyName": "LALA",
            "purchaseOrderLines": [
                {
                    "lineNumber": "10",
                    "itemNumber": "255",
                    "itemDescription": "TestItem2",
                    "unitPrice": 24.64,
                    "quantity": 2,
                    "isServiceBased": false,
                    "taxIndicator1": "LAAA5",
                    "taxIndicator2": "4",
                    "unit": "-",
                    "deliveryLines": [],
                    "supplierItems": [],
                    "isActive": true
                },
                {
                    "lineNumber": "20",
                    "itemNumber": "5555555",
                    "itemDescription": "3test, Ind",
                    "unitPrice": 32.56,
                    "quantity": 2,
                    "isServiceBased": false,
                    "taxIndicator1": "LAAA5",
                    "taxIndicator2": "4",
                    "unit": "-",
                    "deliveryLines": [],
                    "supplierItems": [],
                    "isActive": true
                }
            ],
            "orderIdentifier": "261656",
            "supplierName": "Lopes BVBA",
            "orderType": "T",
            "isActive": true
        },
        {
            "supplierId": "5555",
            "currencyCode": "EUR",
            "companyId": "API",
            "companyName": "LALA2",
            "purchaseOrderLines": [
                {
                    "lineNumber": "1",
                    "itemNumber": "448",
                    "itemDescription": "TestItem",
                    "unitPrice": 1563.23117,
                    "quantity": 1,
                    "isServiceBased": false,
                    "unit": "-",
                    "deliveryLines": [],
                    "supplierItems": [],
                    "isActive": true
                },
                {
                    "lineNumber": "2",
                    "itemNumber": "5551",
                    "itemDescription": "Test",
                    "unitPrice": 524.92539,
                    "quantity": 1,
                    "isServiceBased": false,
                    "unit": "-",
                    "deliveryLines": [],
                    "supplierItems": [],
                    "isActive": true
                }
            ],
            "orderIdentifier": "84615",
            "supplierName": "CLopes.",
            "orderType": "T",
            "isActive": true
        }]
Run Code Online (Sandbox Code Playgroud)

我创建了以下模型类:

public class purchaseOrder
{
        public string supplierId { get; set; }
        public string currencyCode { get; set; }
        public string companyId { get; set; }
        public string companyName { get; set; }
        public List<purchaseOrderLines> purchaseOrderLines { get; set; }
        public double orderIdentifier { get; set; }
        public string supplierName { get; set; }
        public string Ordertype { get; set; }
        public bool isActive { get; set; }
}

public class purchaseOrderLines
{
        public int lineNumber { get; set; }
        public string itemnumber { get; set; }
        public string itemDescription { get; set; }
        public double unitPrice { get; set; }
        public int quantity { get; set; }
        public bool isServiceBased { get; set; }
        public string unit { get; set; }  
        public List<deliveryLines> deliveryLines { get; set; }
        public string[] supplierItems { get; set; }
        public bool isActive { get; set; }
}

public class deliveryLines
{
        public int deliveredQuantity { get; set; }
        public DateTime? deliveredDate { get; set; }
        public string   deliveryNote { get; set; }
        public bool isActive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我尝试通过将字符串反序列化为purchaseOrder来做到这一点

(purchaseOrder purchaseOrderobject  = JsonConvert.DeserializeObject<purchaseOrder >(json);)
Run Code Online (Sandbox Code Playgroud)

但没有成功。我想也许我必须使用字典来做到这一点,但我不完全确定该怎么做。

有没有办法通过逐一获取 json 对象并反序列化它们来实现此目的,如以下链接所示?

反序列化单个对象

Mat*_*ves 6

你说“没有成功”,所以不清楚是否有错误或什么。但...

我认为问题是你的根对象。我通过json2csharp.com运行了你的 JSON ,结果如下:

public class PurchaseOrderLine
{
    public string lineNumber { get; set; }
    public string itemNumber { get; set; }
    public string itemDescription { get; set; }
    public double unitPrice { get; set; }
    public int quantity { get; set; }
    public bool isServiceBased { get; set; }
    public string taxIndicator1 { get; set; }
    public string taxIndicator2 { get; set; }
    public string unit { get; set; }
    public List<object> deliveryLines { get; set; }
    public List<object> supplierItems { get; set; }
    public bool isActive { get; set; }
}

public class PurchaseOrder
{
    public string supplierId { get; set; }
    public string currencyCode { get; set; }
    public string companyId { get; set; }
    public string companyName { get; set; }
    public List<PurchaseOrderLine> purchaseOrderLines { get; set; }
    public string orderIdentifier { get; set; }
    public string supplierName { get; set; }
    public string orderType { get; set; }
    public bool isActive { get; set; }
}

public class RootObject
{
    public List<PurchaseOrder> purchaseOrders { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

尝试一下var root = JsonConvert.DeserializeObject<RootObject>(json);