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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM