2011-11-06 79 views
0

我有一個要求,即必須通過連接到API來獲取一些數據。java-無法將查詢返回的對象映射到正確定義的類

我已映射通過查詢返回到散列映射該目的,使用以下代碼 -

 untypedResult=wt.QueryPermissions(); 
     resp.getWriter().println(" Response for QueryPermissions----"); 
     if(wt.errormsg=="No Error") 
     { 
      hMap = (HashMap<String, Integer>) untypedResult; 

      Set set = hMap.entrySet(); 

      Iterator i = set.iterator(); 

      while(i.hasNext()){ 
       Map.Entry me = (Map.Entry)i.next(); 
       resp.getWriter().println(me.getKey() + " : " +  me.getValue()); 

輸出通過使用上述代碼作爲返回 -

Response for QueryPermissions---- 
get_all_words_popularity : {keyphrase_limit=999, timeout_limit=99, cost_per_call=99, result_limit=999} 

現在我試圖將響應(在對象中)映射到以下類---

public class wt_queryperm_class { 

public Integer keyphrase_limit; 
public Integer timeout_limit; 
public Integer cost_per_call; 
public Integer result_limit; 


} 

此外,現在我修改了代碼編輯要顯示的數據,如圖below--

//declare new object to store result of QueryPermissions 
     wt_queryperm_class a; 

    untypedResult=wt.QueryPermissions(); 
     resp.getWriter().println(" Response for QueryPermissions----"); 
     if(wt.errormsg=="No Error") 
     { 
      hMap = (HashMap<String, Integer>) untypedResult; 

      Set set = hMap.entrySet(); 

      Iterator i = set.iterator(); 

      while(i.hasNext()){ 
       Map.Entry me = (Map.Entry)i.next(); 
       a= (wt_queryperm_class)(me.getValue()); 
       resp.getWriter().println(me.getKey() + " : Cost per call=" + a.cost_per_call + "Keyphrase limit=" + a.keyphrase_limit + " Result limit=" + a.result_limit +" Timeout limit=" + a.timeout_limit); 
      } 

不過,我得到以下錯誤,當我運行上面的代碼 -

Problem accessing /keywords_trial_application. Reason: 

java.util.HashMap cannot be cast to com.taurusseo.keywords.wt_queryperm_class 
Caused by: 

java.lang.ClassCastException: java.util.HashMap cannot be cast to com.taurusseo.keywords.wt_queryperm_class 

什麼我錯在這裏做什麼?如何正確投射響應,以便我可以正確提取4個值中的每一個?

回答

1

查詢的結果,給出了打印內容以及得到的錯誤是Map<String, Map<String, Integer>>(或者如果數字存儲爲字符串,則爲Map<String, Map<String, String>>)。

該地圖有一個單一的鍵:"get_all_words_popularity"。相關的值包含4個鍵:"keyphrase_limit","timeout_limit","cost_per_call","result_limit"

所以你的代碼,而像這樣:

untypedResult = wt.queryPermissions(); 
resp.getWriter().println(" Response for QueryPermissions----"); 
if("No Error".equals(wt.errormsg)) { 
    Map<String, Map<String, Integer> hMap = 
     (Map<String, Map<String, Integer>) untypedResult; 

    for (Map.Entry<String, Map<String, Integer>> me : hMap.entrySet()) { 
     Map<String, Integer> value = me.getValue(); 
     WtQueryPerm perm = new WtQueryPerm(value.get("keyphrase_limit"), 
              value.get("timeout_limit"), 
              value.get("cost_per_call"), 
              value.get("result_limit")); 
     resp.getWriter().println(me.getKey() 
           + " : Cost per call=" + perm.getCostPerCall() 
           + ", Keyphrase limit=" + perm.getKeyphraseLimit() 
           + ", Result limit=" + perm.getResultLimit() 
           + ", Timeout limit=" + perm.getTimeoutLimit()); 

    } 
} 

請注意,我的代碼

  • 方面的Java命名約定
  • 總是使用泛型類型,避免了管型和編譯警告
  • 不會將字符串與==進行比較,而是使用等於,因爲==只比較引用而不是值。
  • 僅在需要時聲明具有較小範圍的變量。
  • 使用getters訪問構造對象的狀態。

也明白,強制轉換不會奇蹟般地將對象轉換爲另一種對象類型。它只是允許將A類型的對象引用爲另一類型B,並且僅在對象實際上是B類型(並且因此B具有A作爲祖先或接口)時才起作用

+0

謝謝你,你的回答很有啓發! – Arvind

0

看起來像您想從一個Bean中填充Bean HashMap中。雅加達的Beanutils爲此提供幫助。

wt_queryperm_class bean=new wt_queryperm_class(); 
BeanUtils.populate(bean, hmap);