java - How do you implement recursion for comments? -
i'm trying parse reddit comments, , stuck on how of comment replies in 1 call.
a reddit comment object begins array ["data"]["children"]
. loop through that, , ["children"]["replies"]["data"]["children"]
array gives of sub-comment replies. can grab more comments (if available) ["replies"]["data"]["children"]
array.
right now, have hard-coded retrieves 3 levels deep. how make dynamic keeps on retrieving until there no more comments?
here code have far not dynamic:
jsonobject data = commentsobj.get("data").asobject(); jsonarray children = data.get("children").asarray(); final treenode root = treenode.root(); (int i=0; < 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"); (int a=0; < morecomments.size(); a++) { jsonobject = 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"); (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); } } } } }
this android project, , treenode variable view setting up. thanks
well, you've answered question in title - recursion :)
i'm not going give actual code, since reddit (and android) specific , don't use reddit (and android), looks regular recursive dfs ok.
java pseudocode:
treeelementclass root = getelementrecursive(data) treeelementclass getelementrecursive(jsonobject obj){ treeelementclass parent = new treeelementclass(); ...some initialization of parent element... for(jsonobject child : obj.get("children").asarray()){ parent.addchild(getelementrecursive(child)) } return parent; }
Comments
Post a Comment