posts - 30,  comments - 32,  views - 45万
< 2025年6月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 1 2 3 4 5
6 7 8 9 10 11 12
复制代码
 public static <T> T obj2Obj(Object origin, Class<T> clazz) {
Gson gson = new Gson();
if(origin == null) { return null; } return gson.fromJson(gson.toJson(origin), clazz); }
//把一个list对象转成含有相同属性的另外一个list
public static <C extends Collection<T>, T> C obj2List(Object origin, Class<T> type) {
Gson gson = new Gson();
if(origin == null) { return null; } Type[] argumentTypes = new Type[]{type}; Type parameterizedType = ParameterizedTypeImpl.make(List.class, argumentTypes, null); return gson.fromJson(gson.toJson(origin), parameterizedType); }
复制代码

 

public static <T> T obj2Obj(@C("待转对象")Object origin, @C("转成的类")Class<T> clazz) {
if(origin == null) {
return null;
}
return gson.fromJson(gson.toJson(origin), clazz);
}

public static <C extends Collection<T>, T> C obj2List(Object origin, Class<T> type) {
if(origin == null) {
return null;
}

Type[] argumentTypes = new Type[]{type};
Type parameterizedType = ParameterizedTypeImpl.make(List.class, argumentTypes, null);
return gson.fromJson(gson.toJson(origin), parameterizedType);
}
posted on   rising1234  阅读(463)  评论(0)    收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示