0

Trying to deserialize an api repsonse like this:

{
  "Afghanistan": {
    "All": {
      "confirmed": 157725,
      "recovered": 0,
      "deaths": 7332,
      "country": "Afghanistan",
      "population": 35530081,
      "sq_km_area": 652090,
      "life_expectancy": "45.9",
      "elevation_in_meters": null,
      "continent": "Asia",
      "abbreviation": "AF",
      "location": "Southern and Central Asia",
      "iso": 4,
      "capital_city": "Kabul",
      "lat": "33.93911",
      "long": "67.709953",
      "updated": "2021-12-17 04:22:14"
    }
  },
  "Albania": {
    "All": {
      "confirmed": 204928,
      "recovered": 0,
      "deaths": 3156,
      "country": "Albania",
      "population": 2930187,
      "sq_km_area": 28748,
      "life_expectancy": "71.6",
      "elevation_in_meters": null,
      "continent": "Europe",
      "abbreviation": "AL",
      "location": "Southern Europe",
      "iso": 8,
      "capital_city": "Tirana",
      "lat": "41.1533",
      "long": "20.1683",
      "updated": "2021-12-17 04:22:14"
    }
  },
  "Algeria": {
    "All": {
      ...
    }
  }
}

and so on up to ~200 countries.

The problem is the format that implies me to trait each country as an individual object:

public class ApiResponse {
    private Afghanistan afghanistan;
    private Afghanistan albania;
    private Afghanistan algeria;
    private Afghanistan andorra;
    private Afghanistan angola;
    private Afghanistan antiguaAndBarbuda;
    private Afghanistan argentina;
    private Afghanistan armenia;
    private Australia australia;
    ...

Hence I would like to have an array of countries, say:

{
  "countries":[    
    {
    "All": {
      "confirmed": 157725,
      "recovered": 0,
      "deaths": 7332,
      "country": "Afghanistan",
      "population": 35530081,
      "sq_km_area": 652090,
      "life_expectancy": "45.9",
      "elevation_in_meters": null,
      "continent": "Asia",
      "abbreviation": "AF",
      "location": "Southern and Central Asia",
      "iso": 4,
      "capital_city": "Kabul",
      "lat": "33.93911",
      "long": "67.709953",
      "updated": "2021-12-17 04:22:14"
    },    
    {
    "All": {
      "confirmed": 204928,
      "recovered": 0,
      "deaths": 3156,
      "country": "Albania",
      "population": 2930187,
      "sq_km_area": 28748,
      "life_expectancy": "71.6",
      "elevation_in_meters": null,
      "continent": "Europe",
      "abbreviation": "AL",
      "location": "Southern Europe",
      "iso": 8,
      "capital_city": "Tirana",
      "lat": "41.1533",
      "long": "20.1683",
      "updated": "2021-12-17 04:22:14"
    }
  ]
}  

I've already started digging into custom deserializers but couldn't figure out how to implement it yet. Appreciate any advice.

  • Which language and technology are you using? – Pine Code Dec 17 '21 at 11:55
  • Kotlin and gson/retrofit2 libraries – CaptainDmitro Dec 17 '21 at 11:57
  • Have you tried [this](https://stackoverflow.com/questions/47982148/usage-of-jackson-jsonproperty-annotation-for-kotlin-data-classes)? – Pine Code Dec 17 '21 at 13:23
  • Could you please check if the `ApiResponse` code you provided is correct? I doubt that nearly all of the fields should be of type `Afghanistan`. Have you tried parsing the data as `Map`? Where the String key is the country name and `CountryData` is a custom class. – Marcono1234 Dec 18 '21 at 19:52
  • Don't, don't and don't use "JSON to POJO" tools. I can't tell how bad its generated outcome is: it's just terrible and you didn't even try to mitigate it. Next, what you're looking for and what such tools aren't aware of is merely java.util.Map: yeah, that dynamic thing allowing you to avoid unnecessary fields in a bi-i-i-i-ig mapping class. Let's go next: what you need is transforming a JSON document A into a JSON document B (at least how I see it), so using JSON trees (JsonElement from Gson) would be just fine as long as you can transform them. No mapping classes required. That's it. – terrorrussia-keeps-killing Dec 19 '21 at 10:09

0 Answers0