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

时间:2018-02-02 14:41:45

标签: c# json serialization

我试图从具有多个对象的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
        }]

我创建了以下模型类:

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

我尝试将字符串反序列化为purchaseOrder

(purchaseOrder purchaseOrderobject  = JsonConvert.DeserializeObject<purchaseOrder >(json);)

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

有没有办法通过逐个获取json对象并将它们反序列化来实现这一点,就像在以下链接中一样?

Deserialize single object

2 个答案:

答案 0 :(得分:1)

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

我认为问题是你的根目标。我通过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; }
}

使用var root = JsonConvert.DeserializeObject<RootObject>(json);

尝试

答案 1 :(得分:0)

您JSON是一个采购订单数组,因此您需要将其反序列化为列表

var list = JsonConvert.DeserializeObject<List<purchaseOrder>>(json);)