【问题标题】:Get value from dynamic json in Android从 Android 中的动态 json 中获取值
【发布时间】: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


    【解决方案1】:

    所以最后我决定不使用改造,因为我找不到我想要的解析 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。希望这会对某人有所帮助

    【讨论】:

      【解决方案2】:

      您可以简单地gson

      在您的项目中导入。

      dependencies {
          compile 'com.google.code.gson:gson:2.8.1'
      }
      
      public class TestModel {
      
          private String name;
          private int age;
          private String position;
      }
      

      用途:

      String strModel ="Staff{name='john', age=35, position='Developer'}"
      Gson gson = new Gson();
      TestModel testModel = gson.fromJson(strModel, TestModel .class);
      

      阅读更多:Samples

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-02
        • 2011-04-21
        • 1970-01-01
        • 1970-01-01
        • 2016-06-22
        • 1970-01-01
        • 2021-01-18
        • 1970-01-01
        相关资源
        最近更新 更多