I have the following JSON string which is received from an API and it has the different property name:
{
"countryitems": [
{
"1": {
"ourid": 1,
"title": "AF",
"code": "AF",
"source": "https://localhost:44301/",
"total_cases": 34366,
"total_recovered": 21135,
"total_unresolved": 0,
"total_deaths": 994,
"total_new_cases_today": 0,
"total_new_deaths_today": 0,
"total_active_cases": 0,
"total_serious_cases": 12237
},
"182": {
"ourid": 193,
"title": "NV",
"code": "NV",
"source": "https://localhost:44301/",
"total_cases": 370,
"total_recovered": 350,
"total_unresolved": 0,
"total_deaths": 0,
"total_new_cases_today": 0,
"total_new_deaths_today": 0,
"total_active_cases": 0,
"total_serious_cases": 20
},
"stat": "ok"
}
]
}
mapping classes:
public class RootObjectCountry
{
public IList<countryitems> countryitems { get; set; }
}
public class countryitems
{
public string stat { get; set; }
public Dictionary<string, Data> results { get; set; }
}
public class Data
{
[JsonProperty(PropertyName = "ourid")]
public int ourid { get; set; }
[JsonProperty(PropertyName = "title")]
public string title { get; set; }
[JsonProperty(PropertyName = "code")]
public string code { get; set; }
[JsonProperty(PropertyName = "total_cases")]
public int total_cases { get; set; }
[JsonProperty(PropertyName = "total_recovered")]
public int total_recovered { get; set; }
[JsonProperty(PropertyName = "total_deaths")]
public int total_deaths { get; set; }
[JsonProperty(PropertyName = "total_new_cases_today")]
public int total_new_cases_today { get; set; }
[JsonProperty(PropertyName = "total_new_deaths_today")]
public int total_new_deaths_today { get; set; }
[JsonProperty(PropertyName = "total_active_cases")]
public int total_active_cases { get; set; }
[JsonProperty(PropertyName = "total_serious_cases")]
public int total_serious_cases { get; set; }
[JsonProperty(PropertyName = "stat")]
public string stat { get; set; }
}
I am using the above mapping classes. Due to the dynamic nature of the attribute the mapping not working as intended
var jsonString = await httpResponse.Content.ReadAsStringAsync();
var CountryData = JsonConvert.DeserializeObject<RootObjectCountry>(jsonString);
How to Deserialize the nested JSON with different property names? Thanks in advance