0

I am using RestSharp to call an API which is returning JSON. I send the API a list of IDs and the API returns info for each ID in some JSON array with key being the ID. What is the best approach to deserialize this dynamic info into C# objects?

var response = client.Get<MyObject>(request); //deserializes JSON response to MyObject

So far I have a class which is able to deserialize the timestamp, status, code, message, and errors fine, but I am not sure how to handle the ids node.

I tried using JSON to C# tools, but they create a new object for each ID in the response.

Ex) Request:

www.apiExample.com/v3/info?ids=1,2,3&market=US

Response:


    {
    "timestamp": 1605486853,
    "status": "SUCCESS",
    "statusCode": 200,
    "statusMessage": "Ok",
    "result": {
        "errors": [],
        "ids": {
            "1": {
                "US": [
                    {
                        "name": "name1",
                        "offer": {
                            "price": {
                                "currencyCode": "USD",
                                "amount": 8.95
                            }
                        }
                    }
                ]
            },
            "2": {
                "US": [
                    {
                        "name": "name2",
                        "offer": {
                            "price": {
                                "currencyCode": "USD",
                                "amount": 7.95
                            }
                        }
                    }
                ]
            },
            "3": {
                "US": [
                    {
                        "name": "name3",
                        "offer": {
                            "price": {
                                "currencyCode": "USD",
                                "amount": 9.95
                            }
                        }
                    }
                ]
            }
        }
    }
}
xMetalDetectorx
  • 160
  • 1
  • 14
  • 3
    Use the tools, and know `ids` is a dictionary... See how you go, I have confidence you can figure it out – TheGeneral Nov 16 '20 at 04:25
  • @TheGeneral I have added Dictionary> ids {get;set;} to my Result object, but it remains null. I was able to deserialize errors correctly by having List errors {get;set;} in my Result object. The Result object is inside a Root object which contains the other info (status, statusCode, etc). Still not sure what I am doing incorrectly at this point. – xMetalDetectorx Nov 16 '20 at 05:46
  • I needed to make it Dictionary>> ids { get; set; } and it worked. Thanks! – xMetalDetectorx Nov 16 '20 at 07:35

1 Answers1

0

You can use the serializer in System.Text.Json to deserialize that part into a Dictionary<>. Below is a simplified example that focuses on the dictionary part.

Using these classes:

public class Container
{
    [JsonPropertyName("ids")]
    public Dictionary<string, SomeClass> Ids { get; set; }
}

public class SomeClass
{
    [JsonPropertyName("name")]
    public string Name { get; set; }

    [JsonPropertyName("code")]
    public string Code { get; set; }
}

And this JSON string:

{
    "ids": {
        "1": {
            "name": "foo",
            "code": "bar"
        },
        "2": {
            "name": "foo2",
            "code": "bar2"
        },
        "3": {
            "name": "foo3",
            "code": "bar4"
        }
    }
}

You can deserialize using:

Container container = System.Text.Json.JsonSerializer.Deserialize<Container>(jsonString);
OJ Raqueño
  • 4,471
  • 2
  • 17
  • 30
  • Thanks for your reply. I don't think i have access to that particular part of JSON since i am using RestSharp to call and deserialize at the same time. (var response = client.Get(request);) I have made an object for previous calls and it deserializes fine, but with this call it contains a dictionary as in my example response. – xMetalDetectorx Nov 16 '20 at 05:53