The threaded comments are stored in Database as flat records with commenId and ParentCommentId. Like below.
commentId : 1
userId : 815
userFName:Joe
userLName:Doe
timeStamp:12345678888
commentText:""
parentCommentId:0
commentId : 2
userId : 615
userFirstName:Ken
userLastName:Tait
timeStamp:12345678988
commentText:"Comment text"
parentCommentId:1
commentId : 3
userId : 415
userFirstName:Brain
userLastName:Dell
timeStamp:12345678
commentText:"Comment text"
parentCommentId:0
I build the Java object using the following Java class
public class Comment {
int commentId;
int userId;
String userFName;
String userLName;
long timeStamp;
String commentText;
int parCommId;
}
List<Comment> comments;
I have the List of comments object. Now I have to traverse the list and convert this list of comment object into nested Json object. The comment objects with parCommId == 0 are the top level comment and the other comment objects (parCommId != 0) should be nested under the commentId of the comment object.
In the above example, the output should be nested like below
CommentId_1
CommentId_2
CommentID_3