3

I'm trying to deserialize a json string from a API call with multiple objects but without much success.

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

I created the following model classes:

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

I tried doing this by deserializing the string into a purchaseOrder

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

but with no success. I think maybe I have to make use of a dictionary for this but I'm not completely sure how to do that.

Is there a way to do this by getting the json objects one by one an deserializing them like in the following link?

Deserialize single object

2
  • What does "no success" mean? It is also not very clear which Json objects you want to get one by one, or why or how. Commented Feb 2, 2018 at 14:47
  • You already have a string (not a stream) so there is no obvious problem with converting it to 1 purchaseOrder and use that. Commented Feb 2, 2018 at 14:51

2 Answers 2

6

You said "with no success", so it's unclear if there's an error or what. But...

I think the issue is your root object. I ran your JSON through json2csharp.com and this is what it came up with:

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

Try that with var root = JsonConvert.DeserializeObject<RootObject>(json);

Sign up to request clarification or add additional context in comments.

1 Comment

This worked. Count seems to be 200 instead of 201 as i expected but i can work with that. Thank you! Will approve the answer when it lets me
3

You JSON is an array of purchase order, so you need to deserialize it into a list

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.