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

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

         List imageInfos = imageFaceService.FindAll();
        List> result = new ArrayList>();
        Map map = null;
        for (ImageInfo img : imageInfos) {
            map = new HashMap();
            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);

你可能感兴趣的:(java)