2

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
  • You can deserialize to Dictionary<string, someObject> Commented Jan 5, 2021 at 12:31
  • you can also convert JSON to class with tools like https://app.quicktype.io/ Commented Jan 5, 2021 at 12:35

2 Answers 2

3

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

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

Comments

0

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
}

1 Comment

@EltonSouza was glad to help!

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.