简单粗暴,如下所示 /** * object转换为指定类型的List * @param obj * @param clazz * @return * @param <T> */ public <T> List<T> objectToList(Object obj, Class<T> clazz) { if (obj instanceof List) { List<Object> objectList = (List<Object>) obj; List<T> dtoList = new ArrayList<>(objectList.size()); for (Object item : objectList) { T convertedDto = convertToObject(item, clazz); dtoList.add(convertedDto); } return dtoList; } else { throw new IllegalArgumentException("Object is not a List."); } } /** * 对象转换为指定Dto * @param obj * @param clazz * @return * @param <T> */ private <T> T convertToObject(Object obj, Class<T> clazz) { return JSON.parseObject(JSON.toJSONString(obj), clazz); }
代码应用