2017-06-22 40 views
1

从动态的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获取日期和值?谢谢

回答

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中。希望这会帮助别人

0

您可以简单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