目录

java.lang.ClassCastException-java.util.HashMap-cannot-be-cast-to-java.lang.String

目录

java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String

用List list 接收的list,获取list长度,循环得到 list.get(i),想得到集合中的某个字段值,作为判断条件,报如下错误,

java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String

at io.renren.modules.equipment.controller.AlaDvcController.queryAlaDvcList(AlaDvcController.java:60)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)

at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)

…………………………..

等错误

试了网上的好多方法都不行,最终参考以前的一个例子解决

解决办法:用new Gson().toJson(list1.get(i))接收

for(int i=0;i<list.size();i++){

System.out.println(“list1.get(i):"+new Gson().toJson(list1.get(i)));

String str =new Gson().toJson(list1.get(i));

JSONObject jo = new JSONObject(new String(str));

System.out.println(jo.get(“XXX”));

}

}

即可得到某个字段的值