Deserializing an abstract class in Gson

前端 未结 4 1989
盖世英雄少女心
盖世英雄少女心 2020-11-27 16:10

I have a tree object in JSON format I\'m trying to deserialize with Gson. Each node contains its child nodes as fields of object type Node. Node is an interface, which has

4条回答
  •  一向
    一向 (楼主)
    2020-11-27 16:26

    You have to use TypeToken class from Google Gson. You will need of course has a generic class T to make it works

    Type fooType = new TypeToken>() {}.getType();
    

    gson.toJson(foo, fooType);

    gson.fromJson(json, fooType);
    

提交回复
热议问题