net.sf.json.JSONArray cannot be cast to [Ljava.lang.Object
程序员文章站
2022-03-10 22:00:50
map获取的值为object类型数组,转int[]报错:net.sf.json.JSONArray cannot be cast to [Ljava.lang.Object数据类型数据类型{"rtd_val": [0, 0, 0, 0, 0, 0, 0, 0],"status": [1, 1, 1, 1, 1, 1, 1, 1]}数据类型为json,通过json获取Integer[] statusVals= Arrays.stream((Object[]) jsonUpdata.get(...
map获取的值为object类型数组,转int[]报错:net.sf.json.JSONArray cannot be cast to [Ljava.lang.Object
数据类型
{
"rtd_val": [0, 0, 0, 0, 0, 0, 0, 0],
"status": [1, 1, 1, 1, 1, 1, 1, 1]
}
数据类型为json,通过json获取
Integer[] statusVals= Arrays.stream((Object[]) jsonUpdata.get("status_val")).map(p -> Integer.valueOf(p.toString())).toArray(Integer[]::new);
报错:
net.sf.json.JSONArray cannot be cast to [Ljava.lang.Object
因为获取到的数据类型是object,但值为object[],所以找了好多方法都没解决,最后曲线救国
/**
* object转int数组
*/
public static int[] objectToByteArray(Object obj) {
JSONArray jsonArray= JSONArray.fromObject(obj);
int length=jsonArray.size();
int[] rtdVals=new int[length];
if(length>0){
for (int i = 0; i < jsonArray.size(); i++) {
rtdVals[i] = (int) jsonArray.get(i);
}
}
return rtdVals;
}
完美解决!
朋友们有更好的方法,欢迎讨论
本文地址:https://blog.csdn.net/m0_37987151/article/details/111946285
推荐阅读
-
解决cannot be cast to javax.servlet.Filter 报错的问题
-
Set集合中的treeSet问题:cannot be cast to java.lang.Comparable;
-
SQL中使用sum()应该程序报错:java.math.BigDecimal cannot be cast to java.lang.Integer
-
java.util.LinkedHashMap cannot be cast to xxx
-
dubbo java.lang.ClassCastException: java.lang.String cannot be cast to java.lan
-
dubbo java.lang.ClassCastException: java.lang.String cannot be cast to java.lan
-
关于Object数组强转成Integer数组的问题:Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
-
关于使用maven + Tomcat7-maven-plugin管理并运行web项目抛出cannot be cast to javax.servlet.Servlet的问题和解决
-
net.sf.json.JSONArray cannot be cast to [Ljava.lang.Object
-
java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx