我可以通过在子类中包含父类以递归方式反序列化json吗?

时间:2018-10-16 05:09:55

标签: java json parsing gson

我有一个类似json的

{
    "author":"jack",
    "comment_body":"any message body",
    "replies":{
        "author":"john",
        "comment_body":" reply body",
        "replies": {
            "author":"john",
        "comment_body":" reply body",
        "replies":{
            ...
        }
        }
    }
}

到目前为止我该如何解析这个json

class Comment {
private String author;
private String comment_body;
private Replies replies;
}

class Replies{
private Comment comment_tree;
}

在gson中如何解析评论回复的任何帮助吗?

3 个答案:

答案 0 :(得分:1)

您不需要Replies类。它与您的JSON不匹配。您这里有一个递归类。

首先,您需要稍微编辑JSON,例如(添加null):

{
    "author": "Jack",
    "comment_body": "Any message body",
    "replies": {
        "author": "John",
        "comment_body": "Reply body",
        "replies": {
            "author": "Smith",
            "comment_body": "Another reply body",
            "replies": null
        }
    }
}

接下来,在您的课程中创建一个递归变量:

public class Comment {
    String author;
    String comment_body;
    Comment replies;

    @Override
    public String toString() {
        return "Comment{author='" + author + "', comment_body='" + comment_body + "', replies=" + replies + '}';
    }
}

最后,可运行的类:

import com.google.gson.Gson;

public class Main {
    public static void main (String[] args) {
        String json =   "{\n" +
                        "    \"author\": \"Jack\",\n" +
                        "    \"comment_body\": \"Any message body\",\n" +
                        "    \"replies\": {\n" +
                        "        \"author\": \"John\",\n" +
                        "        \"comment_body\": \"Reply body\",\n" +
                        "        \"replies\": {\n" +
                        "            \"author\": \"Smith\",\n" +
                        "            \"comment_body\": \"Another reply body\",\n" +
                        "            \"replies\": null\n" +
                        "        }\n" +
                        "    }\n" +
                        "}\n";
         Comment comment = new Gson().fromJson(json, Comment.class);
         System.out.println(comment);
    }
}

输出:

Comment{author='Jack', comment_body='Any message body', replies=Comment{author='John', comment_body='Reply body', replies=Comment{author='Smith', comment_body='Another reply body', replies=null}}}

答案 1 :(得分:0)

您将需要进行反思...但是您确实应该考虑使用现有的库,例如Jackson,该库具有ObjectMapper可以为您完成这项工作。

这里是到使用Jackson来对对象进行JSON序列化/反序列化的基础知识的链接。

https://www.baeldung.com/jackson-object-mapper-tutorial

希望有帮助!

答案 2 :(得分:0)

您只需要Comment类。试试这个:

class Comment {
    private String author;
    private String comment_body;
    private Comment replies;
}

示例代码:

public class Main {

    public static void main(String[] args) {
        Comment comment = new Comment();
        comment.setAuthor("Outer Author");
        comment.setReplies(new Comment());
        comment.getReplies().setAuthor("Inner Author");

        System.out.println("Author 1 :"+comment.getAuthor());
        System.out.println("...Author 2 :"+comment.getReplies().getAuthor());
    }
}

class Comment {
    private String author;
    private String comment_body;
    private Comment replies;

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getComment_body() {
        return comment_body;
    }

    public void setComment_body(String comment_body) {
        this.comment_body = comment_body;
    }

    public Comment getReplies() {
        return replies;
    }

    public void setReplies(Comment replies) {
        this.replies = replies;
    }
}

示例输出:

作者1:外部作者
...作者2:内部作者