I am receiving json data in the following format. I have no control over the json data.
{
"response": {
"status": 1,
"httpStatus": 200,
"data": {
"Countries": {
"818": {
"id": "818",
"code": "EG",
"name": "Egypt"
},
"414": {
"id": "414",
"code": "KW",
"name": "Kuwait"
},
"682": {
"id": "682",
"code": "SA",
"name": "Saudi Arabia"
},
"784": {
"id": "784",
"code": "AE",
"name": "United Arab Emirates"
}
},
"Regions": [],
"Cities": [],
"Exclude": []
},
"errors": [],
"errorMessage": null
}
}
I am trying to deserialize the data in order to get the country codes. I used a Dictionary to handle the country id part as the key and a class to handle the other values.
public class CountryResponseData
{
public CountryData Data { get; set; }
}
public class CountryData
{
public Dictionary<string, Country> Countries { get; set; }
}
public class Country
{
public string Code { get; set; }
}
This works fine as long as their are countries in the data, but some of the data is in the following format.
{
"response": {
"status": 1,
"httpStatus": 200,
"data": {
"Countries": [],
"Regions": [],
"Cities": [],
"Exclude": []
},
"errors": [],
"errorMessage": null
}
}
Because the countries are an array instead of an object in this case the deserializer doesn't know how to handle it.
I have tried using a custom json converter like the ones described in these answers (https://stackoverflow.com/a/45505097/16431696 and https://stackoverflow.com/a/48794588/16431696), but it doesn't seem to do anything.
Any help would be much appreciated.