我有一個要求,即必須通過連接到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個值中的每一個?
謝謝你,你的回答很有啓發! – Arvind