1、首先得到JSON数组(一般调用别人的接口,返回的数据是JSON数组)
Object data = result.getData();
System.out.println(data);
[WaterMeter(id=2061, waterMeter=字符串999, waterMeterNo=1000, acquisitionTime=Fri Mar 10 09:34:10 CST 2023, data=1000.200, currentReading=1000.300, consumption=1000.400, createTime=Fri Mar 10 00:00:00 CST 2023, updateTime=Fri Mar 10 00:00:00 CST 2023, gatherDay=null, delFlag=0)]
2、将其先转换成字符串
String s = JSON.toJSONString(data);
System.out.println(s);
[{"acquisitionTime":1678412050000,"consumption":1000.400,"createTime":1678377600000,"currentReading":1000.300,"data":1000.200,"delFlag":0,"id":2061,"updateTime":1678377600000,"waterMeter":"字符串999","waterMeterNo":1000}]
3、再转换成实体类集合
List<WaterMeter> waterMeters = JSON.parseArray(s, WaterMeter.class);
System.out.println(waterMeters);
[WaterMeter(id=2061, waterMeter=字符串999, waterMeterNo=1000, acquisitionTime=Fri Mar 10 09:34:10 CST 2023, data=1000.200, currentReading=1000.300, consumption=1000.400, createTime=Fri Mar 10 00:00:00 CST 2023, updateTime=Fri Mar 10 00:00:00 CST 2023, gatherDay=null, delFlag=0)]
4、调用的接口 返回数据格式
{
"code": "SUCCESS",
"message": "",
"data": [
{
"id": 2061,
"waterMeter": "字符串999",
"waterMeterNo": 1000,
"acquisitionTime": "2023-03-10 09:34:10",
"data": 1000.200,
"currentReading": 1000.300,
"consumption": 1000.400,
"createTime": "2023-03-10 00:00:00",
"updateTime": "2023-03-10 00:00:00",
"gatherDay": null,
"delFlag": 0
}
]
}