gpt4 book ai didi

java - 从 Android 中的动态 json 获取值

转载 作者:行者123 更新时间:2023-11-30 00:29:34 25 4
gpt4 key购买 nike

我想解析下面的动态 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"
}
}
}

这是我的类(class),包含价格、税金和总价

      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接口(interface)中,这是我得到响应的方式

@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 方法给空值。

如何从该 JSON 中获取日期和值?谢谢

最佳答案

所以最后我决定不使用 retrofit,因为我找不到一种方法来按照我的意愿解析 json。我做了什么来解析动态 json 响应

private HashMap<String,JSONObject> getLowfaresJson(JSONObject data){
HashMap<String,JSONObject> result = new HashMap<>();


try {
JSONObject lowfareJson = data.getJSONObject("lowfares");
Iterator keys = lowfareJson.keys();

while ((keys.hasNext())){
//Getting dynamic key from json
String currentDynamicKey = (String) keys.next();

//Getting dynamic value from json
JSONObject currentDynamicValue = lowfareJson.getJSONObject(currentDynamicKey);
result.put(currentDynamicKey,currentDynamicValue);

}


} catch (JSONException e) {
e.printStackTrace();
}

return result;

}

该方法将从动态 json 响应返回 hashmap。希望这会对某人有所帮助

关于java - 从 Android 中的动态 json 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44689928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com