处理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);
    }
相关推荐
oioihoii23 分钟前
深入解析 C++20 中的 std::bind_front:高效函数绑定与参数前置
java·算法·c++20
码递夫32 分钟前
[NO-WX179]基于springboot+微信小程序的在线选课系统
java·spring boot·后端·微信小程序
magic 24533 分钟前
Servlet-http协议、模版方法设计模式、HttpServlet源码分析
java·servlet·tomcat·html·intellij-idea
blammmp36 分钟前
Java EE 进阶:MyBatis-plus
java·java-ee·mybatis
程序员yt37 分钟前
双非一本Java方向,学完感觉Java技术含量不高,考研换方向如何选择?
java·开发语言·考研
小宋要上岸40 分钟前
基于 Qt / HTTP/JSON 的智能天气预报系统测试报告
开发语言·qt·http·json
凌冰_1 小时前
Java 集合中ArrayList与LinkedList的性能比较
java·开发语言
程序猿chen1 小时前
第二重·腾挪篇:云原生轻功身法要诀
java·git·后端·程序人生·云原生·java-ee·github
快来卷java1 小时前
优化MyBatis-Plus批量插入策略
java·windows·spring·tomcat·maven·mybatis
小白天下第一1 小时前
jdk21使用Vosk实现语音文字转换,免费的语音识别
java·人工智能·语音识别