1

In my project i'm trying to reserve a hotel from my app. Now I'm a little stuck at the method to check if the hotel is available at a specific date, because the API response is a json object which has different content. It contains the available room types with their discounts for the time you want to stay. So it's possible that there are all rooms available and i'd get as response something like this:

"rates": {
    "10": {
        "dates": [
            {
                "date": "2018-05-01",
                "amount": 1495.0
            }
        ],
        "total": 1495.0,
        "discounted_total": null
    },
    "5": {
        "dates": [
            {
                "date": "2018-05-01",
                "amount": 1395.0
            }
        ],
        "total": 1395.0,
        "discounted_total": null
    },
    "4": {
        "dates": [
            {
                "date": "2018-05-01",
                "amount": 1295.0
            }
        ],
        "total": 1295.0,
        "discounted_total": null
    },
    "7": {
        "dates": [
            {
                "date": "2018-05-01",
                "amount": 1695.0
            }
        ],
        "total": 1695.0,
        "discounted_total": null
    },
    "6": {
        "dates": [
            {
                "date": "2018-05-01",
                "amount": 1495.0
            }
        ],
        "total": 1495.0,
        "discounted_total": null
    },
    "9": {
        "dates": [
            {
                "date": "2018-05-01",
                "amount": 1795.0
            }
        ],
        "total": 1795.0,
        "discounted_total": null
    },
    "8": {
        "dates": [
            {
                "date": "2018-05-01",
                "amount": 1895.0
            }
        ],
        "total": 1895.0,
        "discounted_total": null
    }
}

4 to 10 are the ids of the available room types. So if there are only the types 6 to 10 available my response for rates would only contain 6 to 10. How am i supposed to serialize the rates object to get all roomtypes that are available?

For now I used

@SerializedName("rates")
private JsonObject rates;

but I'm not sure how i'm supposed to use this now to get all roomtypes. Maybe it's helpful to know that i have a list of available roomtypes as integers in the response too.

I hope someone can help me, thanks in advance.

Community
  • 1
  • 1
Distra
  • 2,260
  • 2
  • 14
  • 23
  • 1
    what do you mean by serialize ? do you want to parse it ? – A.s.ALI Oct 23 '17 at 07:44
  • yes i want to parse it to from the response to an object i call HotelRoomRateData, to access the value rates with hotelRoomRateData.getRates() – Distra Oct 23 '17 at 07:49
  • 1
    Possible duplicate of [Android get JSON key value](https://stackoverflow.com/questions/10154090/android-get-json-key-value) – Vishal Yadav Oct 23 '17 at 07:55
  • yes I think it's an duplicate, thank you for linking it I think I may have my solution now :) – Distra Oct 23 '17 at 08:07

1 Answers1

0

For those who come across this question and don't know how to convert their variable json object to an accessible object to use e.g. hotelRoomRateData.getRates().getRateId(), i'll post my solution to give you an idea of how i've done this:

first i had to create two objects to to which i parse my json object to. Those Objects are called HotelRoomRateData and HotelRoomRateDateData.

HotelRoomRateData looks like this: (it's the rate field to the id of the room type, e.g. 10, 5, etc.)

public class HotelRoomRateData {
    @SerializedName("dates")
    private List<HotelRoomRateDateData> dates;
    @SerializedName("total")
    private Float total;
    @SerializedName("discounted_total")
    private Float discountedTotal;

    public List<HotelRoomRateDateData> getDates() {
        return dates;
    }

    public Float getTotal() {
        return total;
    }

    public Float getDiscountedTotal() {
        return discountedTotal;
    }

    public void setDates(List<HotelRoomRateDateData> dates) {
        this.dates = dates;
    }

    public void setTotal(Float total) {
        this.total = total;
    }

    public void setDiscountedTotal(Float discountedTotal) {
        this.discountedTotal = discountedTotal;
    }

    @Override
    public String toString() {
        return "HotelRoomRateData{" +
                "dates=" + dates +
                ", total=" + total +
                ", discountedTotal=" + discountedTotal +
                '}';
    }
}

HotelRoomRateDateData looks like this: (it's the list in the "dates" field in the json object)

public class HotelRoomRateDateData {
    @SerializedName("date")
    private String date;
    @SerializedName("amount")
    private Float amount;

    public String getDate() {
        return date;
    }

    public Float getAmount() {
        return amount;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setAmount(Float amount) {
        this.amount = amount;
    }

    @Override
    public String toString() {
        return "HotelRoomRateDateData{" +
                "date='" + date + '\'' +
                ", amount=" + amount +
                '}';
    }
}

Then i added private List<HotelRoomRateData> uiUsableRates; with getters and setters to the object in which i am using

@SerializedName("rates") private JsonObject rates;

Now i parsed the content of the json object "rates" to my created objects to access the later in the project inside the response of the server.

JsonObject rates = response.body().getRates();  //get the json object
List<HotelRoomRateData> uiUsableRates = new ArrayList<>(); //create a list in which you're going to save your parsed objects
for (int y = 0; y < response.body().getRoomTypes().size(); y++) {
    String roomTypeIdAsString = response.body().getRoomTypes().get(y).toString(); //get the id of the room, with which you are able to access the different rates for the room types
    JsonObject ratesInside = rates.get(roomTypeIdAsString).getAsJsonObject(); //get the rates object to the room id
    HotelRoomRateData hotelRoomRateData = new HotelRoomRateData(); //create the HotelRoomRateObject to parse the json object 
    hotelRoomRateData.setTotal(ratesInside.get("total").getAsFloat()); 
    JsonArray ratesInsideDates = ratesInside.get("dates").getAsJsonArray(); //get as json array, because it can also be a variable content
    HotelRoomRateDateData hotelRoomRateDateData = new HotelRoomRateDateData(); //create the HotelRoomRateDateObject to parse the json object 
    List<HotelRoomRateDateData> hotelRoomRateDateDataList = new ArrayList<>(); //create a list to which the objects can be added
    for (int i = 0; i < ratesInsideDates.size(); i++) {
        JsonObject dateData = ratesInsideDates.get(i).getAsJsonObject(); //go through the list and set the values
        hotelRoomRateDateData.setDate(dateData.get("date").getAsString());
        hotelRoomRateDateData.setAmount(dateData.get("amount").getAsFloat());
        hotelRoomRateDateDataList.add(hotelRoomRateDateData); //add the parsed object to the list
    }
    hotelRoomRateData.setDates(hotelRoomRateDateDataList); //set the list to the HotelRoomRateData object
    uiUsableRates.add(hotelRoomRateData); //add the parsed object to your list
    Log.i(TAG, uiUsableRates.toString());
}
response.body().setUiUsableRates(uiUsableRates); //set the parsed list to use it later inside the project

I hope this'll help someone later on, happy coding :)

Distra
  • 2,260
  • 2
  • 14
  • 23