0

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

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Would making `countryitems` have in indexer instead have the same effect? `public Data this[string id] { ... }` – Jeff Mercado Jul 12 '20 at 09:26
  • Looks to be a duplicate of [How to deserialize a child object with dynamic (numeric) key names?](https://stackoverflow.com/q/40088941/3744182), and the same answer should work for you. Use a `public Dictionary Data { get; set; }` for teh variably named properties mark it with `[JsonTypedExtensionData]` from the accepted answer, and use `[JsonConverter(typeof(TypedExtensionDataConverter))]` on `countryitems`. – dbc Jul 12 '20 at 14:17
  • Yes, that works. Demo fiddle here: https://dotnetfiddle.net/8JPQPt – dbc Jul 12 '20 at 14:23

0 Answers0