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

标签 java android json retrofit2

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

相关文章:

python - 从 json 中删除\r

java - PipedInputStream 如何从 OutputStream 读取?

java - 从不同包中的类获取 Activity 类中的变量的最佳方法?

android - Android 中 Gridview 中的 fragment

arrays - SWIFT 解析问题

python - 如何将带有反斜杠的字符串转换为json

Java JsonNode 删除中间一个容器

java - 访问另一个类的ArrayList

java - 如何知道一个 Actor 是否存在于 Actor 系统中

android - 在顶部更新时 RecyclerView 崩溃