I want serialize a JSON response in Android with Retrofit and GSON libraries. This response contains an object with "flights" key that can have one or two nested object with different keys. It can be like this:
{
"status": "OK",
"session_id": "A4ZCUaH6GZbxtfi72Qo1uciXFdzOLte1kYa6FSa73chbfglEvNbiLok5x8EwYScqlPZm8jhchFH1662752205",
"flight_type": "oneway",
"flights": {
"oneway": {
"company_name": "Ryanair",
"departure_country": "Italia",
"departure_airport": "Roma (Fiumicino)",
"booking_date": "2022-09-09",
"flight_date": "2022-09-12",
"flight_time": "00:15:00",
"arrival_country": "Regno Unito",
"arrival_airport": "Londra (Gatwick)",
"adults": 2,
"teenagers": 2,
"children": 0,
"newborns": 0,
"flight_price": "190.08"
}
}
}
or like this:
{
"status": "OK",
"session_id": "kYA14e7Jeut3L9SK1AAx0QLso06K0oaxOxkqGKtiLldFvKG9ywXV4IdTWV6wJegz1jxwqeE7D401662753635",
"flight_type": "roundtrip",
"flights": {
"outbound": {
"company_name": "Lufthansa",
"departure_country": "Spagna",
"departure_airport": "Madrid",
"booking_date": "2022-09-09",
"flight_date": "2022-09-14",
"flight_time": "00:45:00",
"arrival_country": "Francia",
"arrival_airport": "Parigi (Vatry)",
"adults": 3,
"teenagers": 0,
"children": 1,
"newborns": 1,
"flight_price": "129.49"
},
"return": {
"company_name": "Lufthansa",
"departure_country": "Francia",
"departure_airport": "Parigi (Vatry)",
"booking_date": "2022-09-09",
"flight_date": "2022-09-19",
"flight_time": "18:30:00",
"arrival_country": "Spagna",
"arrival_airport": "Madrid",
"adults": 3,
"teenagers": 0,
"children": 1,
"newborns": 1,
"flight_price": "138.24"
}
}
}
This is my Android code to serialize the JSON:
this.retrofit = new Retrofit.Builder()
.baseUrl(Globals.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
this.ffi = this.retrofit.create(FlightsFragmentInterface.class);
@POST(Globals.API_ROUTES_PREFIX+"/flightprice")
Call<FlightInfo> getFlightPrice(@Body FlightSearch fs);
The FlightInfo class:
public class FlightInfo {
@SerializedName("session_id")
public String session_id;
@SerializedName("flight_type")
public String flight_type;
@SerializedName("flights")
public HashMap<String, TicketPreview> flights;
}
The TicketPreview class:
public class TicketPreview {
@SerializedName("company_name")
public String company_name;
@SerializedName("departure_country")
public String departure_country;
@SerializedName("departure_airport")
public String departure_airport;
@SerializedName("booking_date")
public String booking_date;
@SerializedName("flight_date")
public String flight_date;
@SerializedName("flight_time")
public String flight_time;
@SerializedName("arrival_country")
public String arrival_country;
@SerializedName("arrival_airport")
public String arrival_airport;
@SerializedName("adults")
public int adults;
@SerializedName("teenagers")
public int teenagers;
@SerializedName("children")
public int children;
@SerializedName("newborns")
public int newborns;
@SerializedName("flight_price")
public float flight_price;
}
How can I set a POJO class to have the flights HashMap property of the FlightInfo class with this key-value structure:
"oneway" : TicketPreview
or:
"outbound" : TicketPreview
"return" : TicketPreview
and assign the JSON object properties of "oneway"/"outbound"/"return" keys to TicketPreview properties?