protected List < CommentTreeElement > parseRecursive ( JSONObject main ) throws RedditParseException { List < CommentTreeElement > commentTree = new ArrayList < CommentTreeElement > ( ) ; // Iterate over the comment tree results JSONArray array = ( JSONArray ) ( ( JSONObject ) main . get ( "data" ) ) . get ( "children" ) ; for ( Object element : array ) { // Get the element JSONObject data = ( JSONObject ) element ; // Make sure it is of the correct kind String kind = safeJsonToString ( data . get ( "kind" ) ) ; // If it is a comment if ( kind != null && kind . equals ( Kind . COMMENT . value ( ) ) ) { // Create comment Comment comment = new Comment ( ( JSONObject ) data . get ( "data" ) ) ; // Retrieve replies Object replies = ( ( JSONObject ) data . get ( "data" ) ) . get ( "replies" ) ; // If it is an JSON object if ( replies instanceof JSONObject ) { comment . setReplies ( parseRecursive ( ( JSONObject ) replies ) ) ; // If there are no replies, end with an empty one } else { comment . setReplies ( new ArrayList < CommentTreeElement > ( ) ) ; } // Add comment to the tree commentTree . add ( comment ) ; } // If it is a more if ( kind != null && kind . equals ( Kind . MORE . value ( ) ) ) { // Add to comment tree commentTree . add ( new More ( ( JSONObject ) data . get ( "data" ) ) ) ; } } return commentTree ; }
Parse a JSON object consisting of comments and add them to the already existing list of comments . This does NOT create a new comment list .