John McLane
John McLane

Reputation: 103

Can I deserialize a json in recursive manner by including parent class in child class?

I have a json something like

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

how can I parse this json so far my classes are

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

class Replies{
private Comment comment_tree;
}

Any help on how to parse comment response in gson ?

Upvotes: 0

Views: 278

Answers (3)

Kishita Variya
Kishita Variya

Reputation: 810

You just need Comment class. Try this:

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

Sample Code:

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;
    }
}

Sample output:

Author 1 :Outer Author
...Author 2 :Inner Author

Upvotes: 0

user7339164
user7339164

Reputation:

You don't need Replies class. It doesn't match your JSON. You have one recursive class here.

First of all, you need to edit your JSON a little, for example (added 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
        }
    }
}

Next, make a recursive variable in your class:

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

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

Finally, the runnable class:

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);
    }
}

Output:

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}}}

Upvotes: 1

Tomaz Fernandes
Tomaz Fernandes

Reputation: 2594

You will need reflection to do that... But you really should consider using an existing library such as Jackson, which has ObjectMapper that does this job for you.

Here is a link to the basics of using Jackson to serialize / deserialize objects to and from JSON.

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

Hope it helps!

Upvotes: 0

Related Questions