java中JSON转含泛型对象

public static void main(String[] args) {
UserDto userDto=new UserDto("test","14");
Result<UserDto> user=new Result<>("success","1",userDto);
String str=JSON.toJSONString(user);
//1
Result<UserDto> result= JSON.parseObject(str,new TypeReference<Result<UserDto>>(){});

//2
Result<UserDto> result3= JSON.parseObject(str,buildType(Result.class,UserDto.class));
System.out.printf(result3.getData().getName());
}

public static ParameterizedTypeImpl buildType(Type...types){
ParameterizedTypeImpl type=null;
for(int i=types.length-1;i>0;i--){
type=new ParameterizedTypeImpl(new Type[]{type==null?types[i]:type},null,types[i-1]);
}
return type;
}
————————————————
版权声明:本文为CSDN博主「秃头侠」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/maqinchaoTnT/article/details/89890431

原文地址:https://www.cnblogs.com/ceshi2016/p/11880244.html