当前位置: 首页 > news >正文

手动解析Collection

即将被解析的json

{"collection": {"templates": [{"data": [{"name": "plantCode","value": "MSHG_KFXHS02"}, {"name": "details","value": [{"plantMedicamentId": 182,"plantCode": "MSHG_KFXHS02","medicamentCode": "MTRL005","medicamentName": "工业硫酸","presetsNumber": "21","dimension": "kg","creatUser": "mshg","creatTime": "2024-07-10 16:02:36","updateUser": "","updateTime": ""}]}]}]}
}

controller层,通过@RequestBody 正常接收

@SneakyThrows@Log@ApiOperation(value = "新增", notes = "装置药剂关联表数据")@PostMapping(value = "/addDemo")public void addDemo(@RequestBody String savePlantMedicamentInfo)throws Exception {plantMedicamentService.addDemoInfo(savePlantMedicamentInfo);}

service 手动解析

这块主要看怎么一层一层的解析数据并完成转换

public void addDemoInfo(String savePlantMedicamentInfo) {Gson gson = new Gson();JsonElement root = gson.fromJson(savePlantMedicamentInfo, JsonElement.class);JsonObject collectionObj = root.getAsJsonObject().getAsJsonObject("collection");JsonArray templatesArray = collectionObj.getAsJsonArray("templates");JsonArray dataObj = templatesArray.get(0).getAsJsonObject().getAsJsonArray("data");int dataSize = 0;for (JsonElement jsonElement : dataObj){if(jsonElement.getAsJsonObject().has("name")){dataSize ++;}}String value = dataObj.get(0).getAsJsonObject().get("value").getAsString();String plantCode = null;List<Detail> detailList = new ArrayList<>();for (int i=0;i<dataSize;i++){if (i==0){plantCode = dataObj.get(i).getAsJsonObject().get("value").getAsString();}else{JsonArray detailsValue = dataObj.get(i).getAsJsonObject().getAsJsonArray("value");Detail[] detail = gson.fromJson(detailsValue, Detail[].class);for (Detail data : detail){Detail detailInfo = new Detail();detailInfo.setPlantMedicamentId(data.getPlantMedicamentId());detailInfo.setPlantCode(data.getPlantCode());detailInfo.setMedicamentName(data.getMedicamentName());detailInfo.setMedicamentCode(data.getMedicamentCode());detailInfo.setPresetsNumber(data.getPresetsNumber());detailInfo.setDimension(data.getDimension());detailInfo.setCreatUser(data.getCreatUser());detailInfo.setCreatTime(data.getCreatTime());detailInfo.setUpdateUser(data.getUpdateUser());detailInfo.setUpdateTime(data.getUpdateTime());detailList.add(detailInfo);}}}JsonArray detailsValue = dataObj.get(1).getAsJsonObject().getAsJsonArray("value");log.info("测试数据如下:" + detailsValue);}

Detail实体

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class Detail {private int plantMedicamentId;private String plantCode;private String medicamentCode;private String medicamentName;private String presetsNumber;private String dimension;private String creatUser;private String creatTime;private String updateUser;private String updateTime;
}

以上内容为举例,可以参考其中的某些步骤

http://www.lryc.cn/news/397307.html

相关文章:

  • list模拟实现【C++】
  • nginx正向代理、反向代理、负载均衡
  • matlab 有倾斜的椭圆函数图像绘制
  • PTK是如何加密WLAN单播数据帧的?
  • Django之登录权限系统
  • rust way step 1
  • 视觉语言模型导论:这篇论文能成为你进军VLM的第一步
  • Postman工具基本使用
  • uni-app三部曲之三: 路由拦截
  • 专注于国产FPGA芯片研发的异格技术Pre-A+轮融资,博将控股再次投资
  • 【python】QWidget父子关系,控件显示优先级原理剖析与应用实战演练
  • CTF php RCE(三)
  • Android 注解的语法原理和使用方法
  • YOLOv10改进 | Conv篇 | 利用FasterBlock二次创新C2f提出一种全新的结构(全网独家首发,参数量下降70W)
  • 实验-ENSP实现防火墙区域策略与用户管理
  • 【游戏客户端】大话slg玩法架构(二)背景地图
  • git-工作场景
  • coco dataset标签数据结构(json文件)
  • GaussDB关键技术原理:高性能(四)
  • 总结之企业微信(一)——创建外部群二维码,用户扫码入群
  • 透视数据治理:企业如何衡量数据治理的效果?
  • ERC20查询操作--获取ERC20 Token的余额
  • Linux运维:MySQL中间件代理服务器,mycat读写分离应用实验
  • css文字自适应宽度动态出现省略号...
  • 边缘计算盒子_B100_Jetson Nano (aarch64)开发环境搭建
  • 【Superset】dashboard 自定义URL
  • 【Linux网络】IP协议{初识/报头/分片/网段划分/子网掩码/私网公网IP/认识网络世界/路由表}
  • 香蕉派BPI-Wifi6迷你路由器公开发售
  • WPF-控件样式设置
  • C++20中的指定初始化器(designated initializers)