2017-06-22 37 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