springboot解析json数组对象

1、post请求json数组对象

[
  
        {
            "name": "2 (12)",
            "creator": "zhjx",
            "createTime": "2019-03-20 10:47:28",
            "updateTime": "2019-03-20 10:47:28",
            "id": null,
            "filePath": "E:\\gdw\\icon\\\\zhjx\\2 (12).jpg",
            "iconStyle": null,
            "imgFormat": "jpg",
            "imgSign": "Mall",
            "imgSize": "534x300",
            "file": null,
            "fileOne": null
        },
        {
            "name": "2 (1)",
            "creator": "zhjx",
            "createTime": "2019-03-20 10:47:28",
            "updateTime": "2019-03-20 10:47:28",
            "id": null,
            "filePath": "E:\\gdw\\icon\\\\zhjx\\2 (1).jpg",
            "iconStyle": null,
            "imgFormat": "jpg",
            "imgSign": "Mall",
            "imgSize": "500x377",
            "file": null,
            "fileOne": null
        },
        {
            "name": "2 (9)",
            "creator": "zhjx",
            "createTime": "2019-03-20 10:47:28",
            "updateTime": "2019-03-20 10:47:28",
            "id": null,
            "filePath": "E:\\gdw\\icon\\\\zhjx\\2 (9).jpg",
            "iconStyle": null,
            "imgFormat": "jpg",
            "imgSign": "Mall",
            "imgSize": "307x220",
            "file": null,
            "fileOne": null
        }
    
]

2、后台解析

@PostMapping("/saveList")
	public ResultBean saveList(@RequestBody String iconEntityList) throws DIException {
		List iconEntities = new ArrayList();
		try {
			String string = iconEntityList;
			if(StringUtils.isEmpty(string)) {
				 throw DIException.build("文件为空不能保存");
			}
			JSONArray jsonArray=JSONArray.fromObject(string);//把前台接收的string数组转化为json数组
			for (int i = 0; i < jsonArray.size(); i++) {//遍历操作转化json对象
				Object obj=jsonArray.get(i);
				JSONObject jsonObject2=JSONObject.fromObject(obj);
				IconEntity ico=(IconEntity)JSONObject.toBean(jsonObject2, IconEntity.class);//转化为实体对象
				iconEntities.add(ico);//添加到list集合中
			}
		} catch (Exception e) {
			throw DIException.build("文件错误",e);
		}
		return ResultBean.ok(iconService.insertList(iconEntities));
		
	}

 

你可能感兴趣的:(SpringBoot)