如何实现评论的递归?

时间:2015-08-16 22:24:45

标签: java android

我正在尝试解析reddit评论,而我仍然坚持如何在一次通话中获得所有评论回复。

reddit注释对象以数组["data"]["children"]开头。你遍历它,然后得到一个["children"]["replies"]["data"]["children"]数组,它给你所有的子评论回复。您还可以从["replies"]["data"]["children"]数组中获取更多评论(如果有) 现在,我对它进行了硬编码,以便最多只检索三个级别。如何让它变得动态,以便继续检索,直到没有更多的评论?

这是我到目前为止的代码,它不是动态的:

    JsonObject data = commentsObj.get("data").asObject(); 

    JsonArray children = data.get("children").asArray(); 

    final TreeNode root = TreeNode.root(); 

    for (int i=0; i < children.size(); i++)
    {
        JsonObject obj = children.get(i).asObject(); 
        JsonObject dataObj = obj.get("data").asObject();            

        String author = fetchWithHandling(dataObj, "author"); 
        String body = fetchWithHandling(dataObj, "body");  
        String likes = fetchWithHandling(dataObj, "likes"); 
        int score = fetchIntWithHandling(dataObj, "score"); 
        String createdUtc = fetchWithHandling(dataObj, "created_utc");

        JsonObject repliesObj = fetchObjWithHandling(dataObj, "replies"); 

        if (author != null && body != null)
        {   
            RedditCommentHolder.RedditItem redditItem = new RedditCommentHolder.RedditItem(author, body, createdUtc, Integer.toString(score), likes, null); 

            TreeNode commentRoot = new TreeNode(redditItem); 

            if (repliesObj != null)
            {
                JsonObject dataRepliesObj = fetchObjWithHandling(repliesObj, "data"); 
                JsonArray moreComments = fetchArrayWithHandling(dataRepliesObj, "children"); 

                for (int a=0; a < moreComments.size(); a++)
                {
                    JsonObject it = moreComments.get(a).asObject();
                    JsonObject dataComments = it.get("data").asObject(); 

                    String newAuthor = fetchWithHandling(dataComments, "author"); 
                    String newBody = fetchWithHandling(dataComments, "body"); 
                    String newLikes = fetchWithHandling(dataComments, "likes"); 
                    String newCreatedUtc = fetchWithHandling(dataComments, "created_utc");
                    int newScore = fetchIntWithHandling(dataComments, "score");

                    JsonObject thirdCommentsData = fetchObjWithHandling(dataComments, "replies"); 

                    if (newAuthor != null && newBody != null)
                    {
                        RedditCommentHolder.RedditItem newRedditItem = new RedditCommentHolder.RedditItem(newAuthor, newBody, newCreatedUtc, Integer.toString(newScore), newLikes, null); 

                        TreeNode subCommentRoot = new TreeNode(newRedditItem); 

                        if (thirdCommentsData != null)
                        {
                            JsonObject thirdData = fetchObjWithHandling(thirdCommentsData, "data");
                            JsonArray thirdChildren = fetchArrayWithHandling(thirdData, "children"); 

                            for (int p=0; p < thirdChildren.size(); p++)
                            {
                                JsonObject thirdIt = thirdChildren.get(p).asObject(); 
                                JsonObject thirdComments = thirdIt.get("data").asObject(); 

                                String thirdAuthor = fetchWithHandling(thirdComments, "author");
                                String thirdBody = fetchWithHandling(thirdComments, "body"); 
                                String thirdLikes = fetchWithHandling(thirdComments, "likes"); 
                                int thirdScore = fetchIntWithHandling(thirdComments, "score");
                                String thirdCreatedUtc = fetchWithHandling(thirdComments, "created_utc");

                                if (thirdAuthor != null && thirdBody != null)
                                {
                                    RedditCommentHolder.RedditItem thirdRedditItem = new RedditCommentHolder.RedditItem(thirdAuthor, thirdBody, thirdCreatedUtc, Integer.toString(thirdScore), thirdLikes, "continue_thread"); 

                                    TreeNode thirdRoot = new TreeNode(thirdRedditItem);
                                    subCommentRoot.addChildren(thirdRoot);
                                }
                            }
                        }

                        commentRoot.addChildren(subCommentRoot); 
                    }
                }
            }
        }
    }

这是来自Android项目,TreeNode变量只是我正在设置的视图。感谢

1 个答案:

答案 0 :(得分:0)

嗯,你已经在标题中回答了你的问题 - 递归:)

我不会给你实际的代码,因为它主要是reddit(和android)特定的,我不使用reddit(和android),但它看起来像一个常规的递归DFS会好的。

Java伪代码:

TreeElementClass root = getElementRecursive(data)

TreeElementClass getElementRecursive(JsonObject obj){
    TreeElementClass parent = new TreeElementClass();
    ...some initialization of the parent element...
    for(JsonObject child : obj.get("children").asArray()){
        parent.addChild(getElementRecursive(child))
    }
    return parent;
}