[英]java.lang.Integer cannot be cast to java.lang.Long
I'm supposed to receive long integer in my web service.我应该在我的 web 服务中收到长 integer 。
long ipInt = (long) obj.get("ipInt");
When I test my program and put ipInt value = 2886872928, it give me success.当我测试我的程序并输入 ipInt 值 = 2886872928 时,它给了我成功。 However, when I test my program and put ipInt value = 167844168, it give me error:
但是,当我测试我的程序并将 ipInt 值 = 167844168 时,它给了我错误:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
The error is point to the above code.错误是指向上面的代码。
FYI, my data is in JSON format:仅供参考,我的数据采用 JSON 格式:
{
"uuID": "user001",
"ipInt": 16744168,
"latiTude": 0,
"longiTude": 0,
}
Is there any suggestion so that I can ensure my code able to receive both ipInteger value?有什么建议可以确保我的代码能够同时接收 ipInteger 值吗?
Both Integer
and Long
are subclasses of Number
, so I suspect you can use: Integer
和Long
都是Number
的子类,所以我怀疑你可以使用:
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.无论
obj.get("ipInt")
返回的值是Integer
引用还是Long
引用,这都应该有效。 It has the downside that it will also silently continue if ipInt
has been specified as a floating point number (eg "ipInt": 1.5
) in the JSON, where you might want to throw an exception instead.它的缺点是,如果在
ipInt
指定为浮点数(例如"ipInt": 1.5
),它也会静默继续,您可能希望在其中抛出异常。
You could use instanceof
instead to check for Long
and Integer
specifically, but it would be pretty ugly.您可以使用
instanceof
来专门检查Long
和Integer
,但这会非常难看。
We don't know what obj.get()
returns so it's hard to say precisely, but when I use such methods that return Number
subclasses, I find it safer to cast it to Number
and call the appropriate xxxValue()
, rather than letting the auto-unboxing throw the ClassCastException
:我们不知道
obj.get()
返回什么,所以很难准确地说,但是当我使用返回Number
子类的此类方法时,我发现将其强制转换为Number
并调用适当的xxxValue()
比让自动拆箱抛出ClassCastException
:
long ipInt = ((Number)obj.get("ipInt")).longValue();
That way, you're doing explicit unboxing to a long
, and are able to cope with data that could include a .
这样,您就可以对
long
进行显式拆箱,并且能够处理可能包含.
, which would return a Float
or Double
instead. , 这将返回一个
Float
或Double
。
You mention the current approach works when you provide a value outside the range of integer, but fails when you are within the integer range.您提到当前方法在您提供 integer 范围之外的值时有效,但在 integer 范围内时失败。 That is an odd behavior for an API, because it seems you need to check the return type yourself.
对于 API 来说,这是一种奇怪的行为,因为您似乎需要自己检查返回类型。 You can do that.
你可以这样做。 The usual way is with
instanceof
.通常的方法是使用
instanceof
。 Something like,就像是,
long ipInt;
Object o = obj.get("ipInt");
if (o instanceof Integer) {
ipInt = ((Integer) o).intValue();
} else if (o instanceof Long) {
ipInt = ((Long) o).longValue();
}
Long.valueOf(jo.get("ipInt").toString());
Is ok.没关系。
public static void main(String[] args) {
JSONObject jo = JSON.parseObject(
"{ \"uuID\": \"user001\", \"ipInt\": 16744168, \"latiTude\": 0, \"longiTude\": 0}");
System.out.println(jo);
long sellerId1 = Long.valueOf(jo.get("ipInt").toString());
//Long sellerId1 = (long)jo.get("ipInt");
System.out.println(sellerId1);
}
in kotlin I simply use this:在 kotlin 我只是使用这个:
val myInt: Int = 10
val myLong = myInt.toLong()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.