1、简单对象我们传入对象Class来将JSON字符串转为对象

 private static <T> T fromJson(String result, Class<T> classOfT) {
        if (result == null) {
            return null;
        }
        Gson gson = new Gson();
        return gson.fromJson(result, classOfT);
    }

复杂的泛型需要构建TypeToken

复杂的泛型:

import java.util.List;

public class PageList<T> {
    public int Total;

    public int NoReadCount;

    public List<T> Rows;
}

使用Gson来出来JSON,result为json字符串

 Gson gson = new Gson();
 Type type = new TypeToken<PageList<Message>>() {}.getType();
 final PageList<Message> pageList = gson.fromJson(result, type);

 

相关文章:

  • 2021-09-21
  • 2022-01-16
  • 2021-05-20
  • 2022-02-08
  • 2021-08-08
  • 2022-12-23
  • 2022-03-01
  • 2021-07-18
猜你喜欢
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案