zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    assign、weak
    iOS 使用 github
    iOS 知识点
    thinkphp 具体常量,在view里面使用
    一个php+jquery+json+ajax实例
    php pdo操作
    nginx缓存
    php模版静态化技术
    php模版静态化原理
    thinkphp实现多数据库操作
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/11880244.html
Copyright © 2011-2022 走看看