【发布时间】:2017-06-22 04:04:25
【问题描述】:
我想解析下面的动态 JSON
{
"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"
}
}
}
这是我的课程,包含价格、税金和总价
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
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;
}
}
在 API 接口中,这是我获取响应的方式
@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();
}
});
我想获取价格、税金、总价。但是使用我的方法,我尝试了 getPrice 方法给出 null 值。
如何从该 JSON 中获取日期和值?谢谢
【问题讨论】:
标签: java android json retrofit2