处理json,将接口返回的数据转成list<T>,和几个时间处理方法的工具类

  1. 接口或者其他方式返回json格式,也可以直接处理里边只有list的json数据
java 复制代码
//第一种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
    }
}
//需要先获取到records这一层的数据,获取方法:
//调取接口数据
Result<?> listByPaging = apiController.getListByPaging(util);
//获取到data这一层的数据
String oldData = listByPaging.getData().toString();
//将其转成jsonObject对象
JSONObject jsonObject = JSONObject.parseObject(oldData);
//再获取records这一层数据,此处的数据就将是我们最后用于转成list的数据
String data = jsonObject.get("records").toString();

//第二种json格式,不包含分页,那么只需要在上边的的开始只获取oldData这一层数据即可。

//注:我们不仅可以把数据转成list,可以转成好多种类
  1. 处理数据类及方法,T就是要转成list后的对应实体类的泛型
java 复制代码
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);
            //下边是直接将数据处理成Result<?>这种格式,好处在于不需要上边的处理,我们直接把接口放回的数据放进来就可以,缺点在于分页的不好处理
//            JavaType javaType = typeFactory.constructParametricType(Result.class, collectionType);
            List<T> result = objectMapper.readValue(data, collectionType);
            o = result;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return o;
    }
  1. 其他一些时间处理工具方法
java 复制代码
/**
     * 获取今天之前i天的日期
     *
     * @param i
     * @return
     */
    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";
    }

    /**
     * 获取3分钟之前时间
     *
     * @return
     */
    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);// 3分钟之前的时间
            beforeTime.add(Calendar.SECOND, +1); //1秒后
            beforeD = beforeTime.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(beforeD);
    }

    /**
     * 获取一天前时间
     *
     * @return
     */
    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);// 1天前
            beforeTime.add(Calendar.SECOND, +1); //1秒后
            beforeD = beforeTime.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(beforeD);
    }

    /**
     * 获取一周前时间
     *
     * @return
     */
    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);// 1周前
            beforeD = beforeTime.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(beforeD);
    }
相关推荐
gs801402 分钟前
检查当前 Docker 使用的 默认运行时(default runtime)方法
java·开发语言·eureka
hello_ejb34 分钟前
聊聊Spring AI autoconfigure模块的拆分
java·人工智能·spring
wolfengi31 分钟前
Idea Code Templates配置
java·ide·intellij-idea
Arenaschi35 分钟前
运用fmpeg写一个背英文单词的demo带翻译
java·笔记·tcp/ip·其他·eclipse·maven
一只蒟蒻ovo1 小时前
操作系统导论——第26章 并发:介绍
java·开发语言
老华带你飞1 小时前
音乐网站|基于SprinBoot+vue的音乐网站(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·音乐网站
熊文豪1 小时前
Java+Selenium+快代理实现高效爬虫
java·爬虫·selenium·隧道代理·快代理
purrrew2 小时前
【Java ee 初阶】多线程(8)
java·java-ee
TPBoreas5 小时前
Jenkins 改完端口号启动不起来了
java·开发语言
金斗潼关5 小时前
SpringCloud GateWay网关
java·spring cloud·gateway