我从API获取评论对象。每条评论都可以包含其他评论的列表。
这是获取的json:
{
"author": "David",
"body": "this is a comment",
"replies": [
{
"author" : "Bob",
"body" : "this is a comment/reply",
"replies" : [...]
},
...
]
}
这是评论对象:
public class Comment implements Serializable {
String authorName;
String body;
List<Comment> replies;
}
我想将API返回的JSON字符串反序列化为类似于this的CommentTreeStructure对象。
这是上面链接的界面
public interface Tree <N extends Serializable> extends Serializable {
public List<N> getRoots ();
public N getParent (N node);
public List<N> getChildren (N node);
}
public interface MutableTree <N extends Serializable> extends Tree<N> {
public boolean add (N parent, N node);
public boolean remove (N node, boolean cascade);
}
我应该如何使用GSON做到这一点?
将Tree数据结构序列化为JSON怎么样?