- 接口或者其他方式返回json格式,也可以直接处理里边只有list的json数据
{
"code": 200,
"msg": null,
"data": {
"records": [
{
"风速": "0.0",
"电流": "-100.00",
"数据时间": "2025-03-21 14:04:05"
},
{
"风速": "0.0",
"电流": "-100.00",
"数据时间": "2025-03-21 14:07:07"
},
{
"风速": "23.00",
"电流": "-100.00",
"数据时间": "2025-03-21 14:06:05"
}
],
"total": 5,
"size": 10,
"current": 1,
"orders": [],
"optimizeCountSql": true,
"searchCount": true,
"countId": null,
"maxLimit": null,
"pages": 1
}
}
Result<?> listByPaging = apiController.getListByPaging(util);
String oldData = listByPaging.getData().toString();
JSONObject jsonObject = JSONObject.parseObject(oldData);
String data = jsonObject.get("records").toString();
- 处理数据类及方法,T就是要转成list后的对应实体类的泛型
public class PersonEnvironmentDataUtil<T> {
public List<T> getDataList(String data, Class<T> tClass) {
List<T> o = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
JavaType collectionType = typeFactory.constructCollectionType(List.class, tClass);
List<T> result = objectMapper.readValue(data, collectionType);
o = result;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return o;
}
- 其他一些时间处理工具方法
public static String getDateWithBefore(int i) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = org.apache.commons.lang3.time.DateUtils.addDays(new Date(), -i);
return sdf.format(date) + " 00:00:00";
}
public static String get3MinBefore(String time) {
SimpleDateFormat sdf = null;
Date beforeD = null;
try {
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = sdf.parse(time);
Calendar beforeTime = Calendar.getInstance();
beforeTime.setTime(parse);
beforeTime.add(Calendar.MINUTE, -3);
beforeTime.add(Calendar.SECOND, +1);
beforeD = beforeTime.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return sdf.format(beforeD);
}
public static String get1DayBefore(String time) {
SimpleDateFormat sdf = null;
Date beforeD = null;
try {
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = sdf.parse(time);
Calendar beforeTime = Calendar.getInstance();
beforeTime.setTime(parse);
beforeTime.add(Calendar.DAY_OF_MONTH, -1);
beforeTime.add(Calendar.SECOND, +1);
beforeD = beforeTime.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return sdf.format(beforeD);
}
public static String get1WeekBefore(String time) {
SimpleDateFormat sdf = null;
Date beforeD = null;
try {
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = sdf.parse(time);
Calendar beforeTime = Calendar.getInstance();
beforeTime.setTime(parse);
beforeTime.add(Calendar.WEEK_OF_YEAR, -1);
beforeD = beforeTime.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return sdf.format(beforeD);
}