今天在处理一二级评论的时候遇到了一个坑 ,Map中的对象转换出现 “”
这张图片是修改前返回给前端的数据,很显然 key-value 前后都有 “/”
原因
- 实体类中重写了toString()方法 Map<String,Object> value 存实体对象 就出问题了,此时可以将toString()去掉,vauel存对象最好使用fastjosn转换一下 map.put(c.getId(),JSONObject.toJSON(实体对象));
2.使用了多个json转换工具这里建议使用fastjson就可以了
//定义一二级集合
List<Map<Map<String,Object>,List<CliProCommUserVo>>> lists = new ArrayList<>();
//获取一级评论
List<CliProCommUserVo> firstList = new ArrayList<>();
Map<String,Object> firstMap = new HashMap<>();
firstMap.put("status",1);
firstMap.put("commentType",1);
firstMap.put("tipsId",productId);
firstList=cliProductCommentMapper.getCliProCommUserList(firstMap);
//获取二级评论 JSONObject.toJSON(c)
for (CliProCommUserVo c:firstList) {
List<CliProCommUserVo> secondList = new ArrayList<>();
Map<String,Object> secondMap = new HashMap<>();
secondMap.put("status",1);
secondMap.put("commentType",2);
secondMap.put("tipsId",c.getTipsId());
secondMap.put("commentId",c.getId());
secondList=cliProductCommentMapper.getCliProCommUserList(secondMap);
Map<Map<String,Object>,List<CliProCommUserVo>> map = new HashMap();
Map<String,Object> mapKey = new HashMap<>();
//设置一级评论下面的二级评论数量
if (secondList.size()>0){
c.setSecondCount(secondList.size());
}
mapKey.put(c.getId(),JSONObject.toJSON(c));
map.put(mapKey,secondList);
lists.add(map);
}
//这里返回将我们封装好的list一二级集合通过阿里的fastJson转换成 json对象返回 JSONObject.toJSONString(list);
return PandResponseUtil.printFastJson("作品评论",lists);
通过处理后