Create object of json when elements are variables

Viewed 55

i need help for deserialize this json on C#.

{
"date": "2021-01-04T16:58:06-03:00",
"resultsInThisPage": 2,
"currentPage": 1,
"totalPages": 1,
"paymentOrders": {
    "3AC525B8EF5F413D86CFDC9318E8B3F8": {
        "code": "3AC525B8EF5F413D86CFDC9318E8B3F8",
        "status": 5,
        "amount": 3.00,
        "grossAmount": 3,
        "lastEventDate": "2021-01-04T16:54:36-03:00",
        "schedulingDate": "2021-01-04T16:54:31-03:00",
        "transactions": [
            {
                "code": "AF63C1B13EF14F318C51119CBE1E5CA4",
                "date": "2021-01-04T16:54:32-03:00",
                "status": 3
            }
        ],
        "discount": {
            "type": "DISCOUNT_PERCENT",
            "value": 0
        }
    },
    "1E5A8F8D35D34938A12281BD45C62DBE": {
        "code": "1E5A8F8D35D34938A12281BD45C62DBE",
        "status": 1,
        "amount": 3.00,
        "grossAmount": 0,
        "lastEventDate": "2021-01-04T16:54:33-03:00",
        "schedulingDate": "2021-02-04T05:54:31-03:00",
        "transactions": [],
        "discount": {
            "type": "DISCOUNT_PERCENT",
            "value": 0
        }
    }
}

}

I´m trying use one Class for make this, but the elements of "paymentOrders" are variables. How is the best way for do this? I need create a object to manipulate these values.

2 Answers

paymentOrders would be a Dictionary<string,SomeObject> where SomeObject represents the object with all the properties like code, status etc

You can use Dictionary<string, SomeType> (or even Dictionary<Guid, SomeType> in your case, based on provided sample) to represent dynamic property names for paymentOrders json object. Something like this should do the trick:

public class Root    {
    // your dynamic prop
    public Dictionary<string, PaymentOrder> paymentOrders { get; set; } 
    public DateTime date { get; set; } 
    // ... the rest o properties
}

public class PaymentOrder    {
    public string code { get; set; } 
    public int status { get; set; } 
    // ... the rest o properties
}
Related