在我的项目中,我试图从我的应用程序预订酒店。现在我有点坚持使用该方法检查酒店是否在特定日期可用,因为API响应是一个具有不同内容的json对象。它包含可用的房间类型以及您想要住宿的折扣。所以可能有所有可用的房间,我会得到这样的响应:
"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到10是可用房间类型的ID。因此,如果只有6到10类型可用,我对费率的回复只会包含6到10.我如何序列化费率对象以获得所有可用的房型?
现在我用
@SerializedName("rates")
private JsonObject rates;
但是我不确定我现在应该如何使用它来获得所有的房间类型。也许知道我在响应中有一个可用的房型列表作为整数也是有帮助的。
我希望有人可以提前帮助我。
答案 0 :(得分:0)
对于那些遇到这个问题并且不知道如何将他们的变量json对象转换为可访问对象的人来说,例如hotelRoomRateData.getRates()。getRateId(),我会发布我的解决方案,让你知道我是怎么做到的:
首先我必须创建两个对象来解析我的json对象。这些对象称为HotelRoomRateData和HotelRoomRateDateData。
HotelRoomRateData如下所示:(它是房间类型ID的费率字段,例如10,5等)
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如下所示:(它是json对象中“dates”字段中的列表)
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 +
'}';
}
}
然后我将private List<HotelRoomRateData> uiUsableRates;
添加了getter和setter到我正在使用的对象
@SerializedName("rates")
private JsonObject rates;
现在我将json对象的内容“rate”解析为我创建的对象,以便在服务器响应中访问项目中的后者。
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
我希望以后可以帮助别人,快乐编码:)