【发布时间】:2019-02-07 05:31:40
【问题描述】:
我正在尝试使用 for 循环遍历 Object 类的 ArrayList 实例:
for(PayloadDTO payloadDTO:payloadDTOList) {
if(payloadDTO.getEntType().equals(CommonConstants.CUSTOMENTTYPEROLEID.RES_ROLE_TYPE)) {
resRoleNameList.add(payloadDTO.getEntName());
}
}
这给了我以下错误:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.alnt.fabric.external.model.PayloadDTO
at com.alnt.fabric.external.rbac.service.ExternalFabricService.loadImpactedUserCount(ExternalFabricService.java:384)
at com.alnt.fabric.external.rbac.controller.ExternalFabricController.loadImpactedUserCount(ExternalFabricController.java:160)
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)
所以我尝试使用下面的代码进行转换:
payloadDTOList = convert(payloadDTOList.toString(), List<PayloadDTO.class>);
public static <T> T convert(String json, Class<T> type) throws ALNTApplicationException {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, type);
} catch (Exception e) {
throw new ALNTApplicationException(FabricComponentErrorCodes.ACTION_HANDLER_ERROR_CODE,"Object conversion error");
}
}
【问题讨论】:
-
你能在这里发布你的迭代循环吗?我认为问题在于您尝试迭代它的方式。
-
@Jainik 添加循环
标签: java linkedhashmap