您的位置:首页 > 教育 > 培训 > 聚名网官方_社区论坛源码_网站优化名词解释_快速排名seo软件

聚名网官方_社区论坛源码_网站优化名词解释_快速排名seo软件

2025/3/29 0:28:42 来源:https://blog.csdn.net/weixin_45893072/article/details/146494077  浏览:    关键词:聚名网官方_社区论坛源码_网站优化名词解释_快速排名seo软件
聚名网官方_社区论坛源码_网站优化名词解释_快速排名seo软件
  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);}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com