從動態的JSON值我要分析此以下動態JSON獲取的Android
{
"lowfares": {
"2017-07-30": {
"price": "1208.00",
"tax": "946.00",
"totalprice": "2154.00"
},
"2017-07-31": {
"price": "1208.00",
"tax": "946.00",
"totalprice": "2154.00"
}
}
}
這是我的類包含價格,稅收和totalprice
public class PriceModel {
@SerializedName("price")
private String price;
@SerializedName("tax")
private String tax;
@SerializedName("totalprice")
private String totalprice;
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getTax() {
return tax;
}
public void setTax(String tax) {
this.tax = tax;
}
public String getTotalPrice() {
return totalprice;
}
public void setTotalPrice(String totalPrice) {
this.totalprice = totalPrice;
}
}
這是我的課包含的HashMap存儲在API接口的響應
public class ResponseModel {
@SerializedName("prices")
@Expose
private Map<String,PriceModel> priceModelMap;
public Map<String, PriceModel> getPriceModelMap() {
return priceModelMap;
}
public void setPriceModelMap(Map<String, PriceModel> priceModelMap) {
this.priceModelMap = priceModelMap;
}
}
,這是我得到的迴應
@GET("getprice/{start}/{end}/1/2")
Call<ResponseModel> getResponse(@Path("start") String start, @Path("end") String end);
和MainActivity,我執行這樣
Call call = apiInterface.getResponse("CRB","IMY");
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
Log.d("TAG",response.code()+" ");
Log.d("TAG","REsponse: "+response.body());
ResponseModel responseModel = (ResponseModel) response.body();
Log.d("TAG","REsponse: "+responseModel.getPriceModelMap());
Map<String, PriceModel> priceModelMap = responseModel.getPriceModelMap();
for (Map.Entry<String,PriceModel> entry : priceModelMap.entrySet()){
String key = entry.getKey();
PriceModel priceModel = entry.getValue();
System.out.println("KEY: "+key+" value: "+priceModel.getPrice());
}
}
@Override
public void onFailure(Call call, Throwable t) {
call.cancel();
}
});
我想要得到的價格,稅收,totalprice。但使用我的方法,我試圖getPrice方法給null值。
如何從該JSON獲取日期和值?謝謝