java.lang.Integer cannot be cast to java.lang.Long

Both Integer and Long are subclasses of Number, so I suspect you can use:

long ipInt = ((Number) obj.get("ipInt")).longValue();

That should work whether the value returned by obj.get("ipInt") is an Integer reference or a Long reference. It has the downside that it will also silently continue if ipInt has been specified as a floating point number (e.g. "ipInt": 1.5) in the JSON, where you might want to throw an exception instead.

You could use instanceof instead to check for Long and Integer specifically, but it would be pretty ugly.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)