将list集合转换成json传给前端

大致思路:
1.先将list集合一条一条的放入map中
2.再用List<Map<String, Object>> result 将之前的map装进来
3.gson.toJson(result)便可以得到json格式的list集合
代码如下:

         List<ImageInfo> imageInfos = imageFaceService.FindAll();
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        for (ImageInfo img : imageInfos) {
            map = new HashMap<String, Object>();
            map.put("image_id", img.getImage_id());
            map.put("image_name", img.getImage_name());
            map.put("image_path", img.getImage_path());
            map.put("image_feature", img.getImage_feature());
            result.add(map);
        }
        Gson gson = new Gson();
        return gson.toJson(result);
    原文作者:这个冬天有点冷
    原文地址: https://blog.csdn.net/qq_42004289/article/details/96431383
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞