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

发布于:2025-03-26 ⋅ 阅读:(21) ⋅ 点赞:(0)
  1. 接口或者其他方式返回json格式,也可以直接处理里边只有list的json数据
//第一种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后的对应实体类的泛型
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. 其他一些时间处理工具方法
/**
     * 获取今天之前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);
    }