2016-04-07 28 views
2

我想嘗試從Long Array中獲取Long的值。但我有此異常:在線程從Java的Long Array異常中獲取Long值

異常「主要」 java.lang.ClassCastException:java.lang.Integer的 不能轉換爲java.lang.Long中

這樣的代碼:

import java.util.List;   
    public class Bar {   
     private List<Long> departments;  

     public List<Long> getDepartments() { 
      return departments; 
     }  

     public void setDepartments(List<Long> departments) { 
      this.departments = departments; 
     }   

    } 


import java.util.List;  
import net.sf.json.JSONObject;  
public class Foo {  
    public static void main(String[] args) {  
     String str = "{\"departments\":[20,22]}"; 
     JSONObject jsonObject = JSONObject.fromObject(str); 
     Bar bar = (Bar) jsonObject.toBean(jsonObject, Bar.class); 
     List<Long> departments = bar.getDepartments(); 
     Long depId = departments.get(0);// Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long 
System.out.println(depId); 

    }  
} 
+1

大概是因爲jsonObject.toBean在運行時注入,而不是JSONArray jsonArray的龍 –

+0

嘗試'整數的列表= jsonObject.getJSONArray(「department」); Collection depts = JSONArray.toCollection(jsonArray,List.class);'如果有幫助。還沒有嘗試過自己。這裏是'toCollection()'方法的JavaDoc:**返回一個列表或一個考慮泛型的集合** – ujulu

回答

1
public static void main(String[] args) { 

    String str = "{\"departments\":[20,22]}"; 
    JSONObject jsonObject = JSONObject.fromObject(str); 
    Bar bar = (Bar) JSONObject.toBean(jsonObject, Bar.class); 
    List departments = bar.getDepartments(); 
    Long i= Long.valueOf(departments.get(0).toString()); 
    System.out.println(i); 
    System.out.println(departments.get(0).getClass().getName()); 

} 

這是一個net.sf JSON鑄造缺陷,只是轉換爲字符串

+0

你可以使用jackson來拋出json拋出異常 – JesseLin

+0

不能強制轉換爲字符串。嘗試字符串s = departments.get(0).toString();拋出相同的異常。 – muammertuzun

+0

'public static void main(String [] args){ String str =「{\」departments \「:[20l,22l]}」; JSONObject JSONObject = JSONObject.fromObject(str); Bar bar =(Bar)JSONObject.toBean(jsonObject,Bar.class); 列出部門= bar.getDepartments(); Long i = Long.valueOf(departments.get(0).toString()); System.out.println(i); System.out.println(departments.get(0).getClass()。getName()); } – JesseLin

0

的你可以嘗試這樣的,

Long depId = (Long) departments.get(0).intValue(); 

intValue返回int,但後來你將它轉換爲Long。

+0

沒有編譯。 – muammertuzun

+0

@MuammerTÜZÜN您可以顯示'departments.get(0)'實際返回的內容,而不會將其存儲到任何變量。只是通過登錄或打印它 –

0

試試這個

Long depId = departments.get(0).longValue(); 

編輯:

Integer i = 9; 
Long l = i.longValue(); 
System.out.println(i + " " + l); 
+0

沒有工作。 線程「主」java.lang.ClassCastException:java.lang.Integer中的異常無法轉換爲java.lang.Long \t at com.test.Foo.main(Foo.java:16) – muammertuzun

+0

@MuammerTÜZÜN它應該工作。請參閱我的編輯部分 – muzahidbechara

+0

departments.get(0)方法的返回類型是Long,但它嘗試返回Integer。這是問題的根源。如果是''部門,則爲 – muammertuzun

0

龍I = Long.valueOf(部門獲得(0)的ToString());將解決您的問題

+0

可能是最不能解決問題的解決方案 –

+0

您的代碼正在工作時;列出部門= ...;但我定義像這樣列表部門= ..;同樣的錯誤還在繼 – muammertuzun

0

toBean()方法產生與List<Integer>不同的類。 由於這個錯誤,我沒有看到任何理由把Integer轉換爲Long。 將部門更改爲List<Integer>或使用另一個JSon解析器。

0

JSON本身沒有概念IntegerLong,它只知道Number。如果數值不是浮點數值,則數值或其列表將被JSONObject放入Integer對象中。

您將列表聲明爲List<Long>與JSONObject無關,因爲此類型信息在編譯的類代碼(類型擦除)中不可用,因此departments僅僅是對象的列表,因此JSONObject很高興填充Integer對象。

你既可以聲明departmentsList<Integer>或使用額外的轉化方法(看看ujulu的評論到您的文章)