2

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);

    }    
}
2
  • 1
    Probably because jsonObject.toBean injects at runtime a List of Integer instead of Long Commented Apr 7, 2016 at 11:37
  • Try 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 the toCollection() method: Returns a List or a Set taking generics into account. Commented Apr 7, 2016 at 14:21

6 Answers 6

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());

}

It's a net.sf json cast bug, just cast to string

Sign up to request clarification or add additional context in comments.

4 Comments

You can use jackson to void json cast exception
Can not cast to string. Trying to String s = departments.get(0).toString(); throws same exception.
```public static void main(String[] args) { String str = "{\"departments\":[20l,22l]}"; 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()); }
any class could to string
0

The you can try like this,

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

Where intValue returns int, but then you cast it to Long.

1 Comment

@MuammerTÜZÜN Can you show what departments.get(0) returns actually without storing it to any variables. Just by logging or printing it
0

Try this

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

EDIT:

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

4 Comments

did not work. Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long at com.test.Foo.main(Foo.java:16)
departments.get(0) method's return type is Long but its try to return Integer. This is the source of the problem.
if departments.get(0) return still a Integer it will be converted to Long by longValue() method. Plz check again and inform me.
@Md. muzahidul Islam Do not think in Java, this is a problem at bytecode level due to a bug in the toBean method.
0

Long i= Long.valueOf(departments.get(0).toString()); will solve your problem

1 Comment

Your code is working when; List departments = ... ; but i defined like this List<Long> departments = .. ; same error continues.
0

The toBean() method produces a different Class with List<Integer>. I do not see any reason to cast an Integer to Long due to this bug. Change departments to List<Integer> or use an another JSon Parser.

Comments

0

JSON itself has not notion of Integer or Long, it just know Number. A Number value - or a list of that - is put into an Integer object by JSONObject if it is not a floating pint value.

The fact that you declared the List as a List<Long>is not relevant for JSONObject, as this generic type information is not available in the compiled class code (type erasure), there departments is just a list of objects and so JSONObject happily fills in Integer objects.

You could either declare departmentsas List<Integer> or use additional transformation methods (have a look at ujulu's comment to your post)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.