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;
}
fastjson String与泛型类型的转换实例
最新推荐文章于 2022-09-03 18:28:47 发布