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

it2022-05-05  96

大致思路: 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);

最新回复(0)