I want to try to get Long value from Long Array. But I've got this exception:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
Code like this :
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);
}
}
JSONArray jsonArray = jsonObject.getJSONArray("department"); Collection<Long> depts = JSONArray.toCollection(jsonArray, List.class);if it helps. Haven't tried myself. Here is the JavaDoc of thetoCollection()method: Returns a List or a Set taking generics into account.