我想要把查询到的List给遍历出来返回给前端树状菜单,但是遍历的时候转换类型报错了,逻辑层代码:
List<Map<String, Object>> metadataList = metadataService.getAllMeatadata(send_parentid);
List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
Map<String, Object> treeMap = null;
//循环遍历
for (Map<String, Object> rowMap : metadataList) {
String metadata_id = String.valueOf(rowMap.get("metadata_id"));
String metadata_name = String.valueOf(rowMap.get("metadata_name")).trim();
String parent_id = String.valueOf(rowMap.get("parent_id"));
String isLeaf = String.valueOf(rowMap.get("isleaf"));
String grade = String.valueOf(rowMap.get("grade"));
String metadata_type = String.valueOf(rowMap.get("metadata_type"));
//JOptionPane.showMessageDialog(null, isLeaf);
treeMap = new HashMap<String, Object>();
treeMap.put("id", metadata_id);
treeMap.put("pId", parent_id);
treeMap.put("name", metadata_name);
treeMap.put("grade", grade);
treeMap.put("isleaf", isLeaf);
treeMap.put("metadata_type", metadata_type);
if (isLeaf.equals("0")) {
treeMap.put("isParent", true);
}
treeList.add(treeMap);
}
业务层://查询所有菜单
List<Map<String, Object>> getAllMeatadata(String send_parentid) throws Exception;
报错:java.lang.ClassCastException: com.gysa.manage.bean.MetadataBean cannot be cast to java.util.Map
是不是可以用其他的方式来接收或者有什么解决办法?麻烦各位大佬了!
根据报错,返回应该是MetadataBean对象,你把Map<String, Object>改为MetadataBean试试
List<MetadataBean> metadataList = metadataService.getAllMeatadata(send_parentid);
List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
Map<String, Object> treeMap = null;
for (MetadataBean bean : metadataList) {
String metadata_id = String.valueOf(bean.get("metadata_id"));
String metadata_name = String.valueOf(bean.get("metadata_name")).trim();
String parent_id = String.valueOf(bean.get("parent_id"));
String isLeaf = String.valueOf(bean.get("isleaf"));
String grade = String.valueOf(bean.get("grade"));
String metadata_type = String.valueOf(bean.get("metadata_type"));
//JOptionPane.showMessageDialog(null, isLeaf);
treeMap = new HashMap<String, Object>();
treeMap.put("id", metadata_id);
treeMap.put("pId", parent_id);
treeMap.put("name", metadata_name);
treeMap.put("grade", grade);
treeMap.put("isleaf", isLeaf);
treeMap.put("metadata_type", metadata_type);
if (isLeaf.equals("0")) {
treeMap.put("isParent", true);
}
treeList.add(treeMap);
}
hhhhhhhhhhhh,谢谢大佬,其实一开始有这么改过,但是后面get属性哪里没继续写,多谢点通,感觉自己蠢到不行
楼主,我说一下我的看法,
谢谢啦,已经解决了,遍历的时候转换类型出错,泛型写错了,应该是用MetadataBean来装的,之前用的map集合所以出现了类型转换的错误,非常感谢您的回答
@ppplum: 不客气~