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<IconEntity> iconEntities = new ArrayList<IconEntity>();
		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));
		
	}

 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值