处理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);
    }
相关推荐
信誓旦旦的程序猿2 分钟前
【Python 量化取数指南 #13】Python 把行情落库:sqlite 一键存,回测随用随取
java·python·股票数据api·股票数据·股票数据api接口·股票api数据接口·股票量化数据api
她的男孩8 分钟前
头像换了三次还是旧图:秒传 + 预签名 URL + 私有文件权限,四层缓存叠出一个 Bug
java·后端·架构
深入云栈8 分钟前
Netty 4.2.x 源码深度解析 (十五):io_uring 传输 —— RingBuffer 与零拷贝 IO
java·后端
MacroZheng11 分钟前
装上这款全能增强插件,DeepSeek Harness瞬间高大上了!
java·人工智能·后端
long、涯13 分钟前
win10安装Maven3.9(图文版)
java·maven
霸道流氓气质14 分钟前
LangGraph式图编排引擎Java实现:状态机模型、Checkpoint恢复与生产级落地实战
java·开发语言·数据库
AI深栈16 分钟前
第 19 章 · AI 工具循环与条件路由:管住 recursionLimit
java·人工智能
后端LV20 分钟前
「一切皆插件」不是口号:手写一个敢上生产的治理管道过滤器
java
小匠石钧知22 分钟前
05_在k8s集群中安装NFS实现ReadWriteMany存储
java·容器·kubernetes·nfs·readwritemany·rwx
余槐i24 分钟前
-m 2g 反而更早 OOM:Docker 内存计数与宿主机空闲口径差异
java·linux·docker·性能优化·cgroup