我有Json我收到并解析好了,但现在有点迷失了如何处理它。
---
- name: main yml file to trigger the whole process
hosts: otherhost
roles:
- { role: run_script, applist: [ 'a', 'b', 'c', 'd' ] }
我已经建模了两个对象:
"success": true,
"data": {
"endingIndex": 1,
"segmentCount": 2,
"startingIndex": 0,
"totalCount": 2,
"comments": [
{
"childCount": 1,
"content": "This is a root Comment",
"id": 2342246,
"parentID": null,
"submissionID": 623234,
"children": {
"endingIndex": 0,
"segmentCount": 1,
"startingIndex": 0,
"totalCount": 1,
"comments": [
{
"childCount": 1,
"content": "This is second Level Comment",
"id": 2342275,
"parentID": 2342246,
"submissionID": 623234,
"children": {
"endingIndex": 0,
"segmentCount": 1,
"startingIndex": 0,
"totalCount": 1,
"comments": [
{
"childCount": 0,
"content": "This is a third Level Comment",
"id": 2342310,
"parentID": 2342246,
"submissionID": 623234,
"children": null
}
]
}
}
]
}
},
{
"childCount": 0,
"content": "This is first Level Comment too!",
"id": 2342298,
"parentID": null,
"submissionID": 623234,
"children": null
}
]
和
public class Segment{
private List<Comment> comments;
private int endingIndex;
private int segmentCount;
private int startingIndex;
private int totalCount;
//getters...
}
我遇到的问题是试图处理数据。我想列出一个交错的评论列表,但是考虑到列表列表,我的大脑有点茫然。
我从Api请求
收到根段public class Comment{
private int childCount;
private String content;
private int id;
private int parentID;
private Segment children;
//getters...
}
然后我想我需要一个
里面的评论列表CommentSegment fetchedCommentList = new CommentSegment(api);
//this should contain all the info
现在我的大脑正在爆炸,我从哪里开始?
答案 0 :(得分:0)
您可以使用队列,然后沿着这样的数据树:
private List<Comment> treeQueue(Segment parent) {
List<Comment> comments = new List<Comment>();
Queue queue = new LinkedList();
for (Comment comment : parent.getcomments()) {
queue.add(comment);
while(!queue.isEmpty()) {
Comment node = (Comment)queue.remove();
comments.add(comment);
if (node.childCount > 0) {
for (Comment child : ((Segment)node.getchildren()).getcomments()) {
queue.add(child);
}
}
}
}
return comments;
}