Json
[ { "id": 4, "dmNum": "111102", "number": "683272", "parentNum": "0", "type": "1", "name": "大圣", "code": "213134", "mDefault": "37", "description": "请问", "isDel": "0", "opFlag": "A", "createdBy": "创建人", "createdTime": "2020-10-15T05:20:17.000+0000", "updatedBy": "删除者", "updatedTime": "2020-10-15T05:28:18.000+0000", "children": null, "parentName": null }, { "id": 5, "dmNum": "111102", "number": "68327201", "parentNum": "683272", "type": "0", "name": "温度数据", "code": "213135", "mDefault": "95", "description": "不知道这是什么", "isDel": "0", "opFlag": "A", "createdBy": "创建人", "createdTime": "2020-10-15T05:20:18.000+0000", "updatedBy": "删除者", "updatedTime": "2020-10-15T05:27:33.000+0000", "children": null, "parentName": null } ]创建 ObjectMapper
public static ObjectMapper mapper = new ObjectMapper(); static { // 转换为格式化的json mapper.enable(SerializationFeature.INDENT_OUTPUT); // 如果json中有新增的字段并且是实体类类中不存在的,不报错 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //修改日期格式 mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));Java代码:
String s1 = httpAPIService .doGet("http://192.168.0.25:8888/modeloutput/selectOutputByModelId?mid=" + mid); ModelOutput[] modelOutput = mapper.readValue(s1, ModelOutput[].class);//输出表对象 for (ModelOutput output : modelOutput) { //赋值:元数据编号 deviceOutput.setMetaNum(output.getNumber()); //赋值;数据编码 deviceOutput.setCode(output.getOutputCode()); deviceOutputMapper.save(deviceOutput); System.out.println(deviceOutput); }这串json数据最外层是[ ],代表为对象数组,因为Jackson object mapper在把返回的json片段转换成对象时。
遍历该数组中的数据。转换成Java对象。 idea快捷键:iter
for (ModelOutput output : modelOutput) { //赋值:元数据编号 deviceOutput.setMetaNum(output.getNumber()); //赋值;数据编码 deviceOutput.setCode(output.getOutputCode()); deviceOutputMapper.save(deviceOutput); System.out.println(deviceOutput); }问题解决。
编写实属不易,若喜欢或者对你有帮助记得点赞+评论+收藏
