Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试在Android中使用嵌套列表对Json进行排序_Java_Android_Json_Nested Lists - Fatal编程技术网

Java 尝试在Android中使用嵌套列表对Json进行排序

Java 尝试在Android中使用嵌套列表对Json进行排序,java,android,json,nested-lists,Java,Android,Json,Nested Lists,我有Json我正在接收和解析Ok,但我现在有点迷失在如何处理它 "success": true, "data": { "endingIndex": 1, "segmentCount": 2, "startingIndex": 0, "totalCount": 2, "comments": [ { "childCount": 1, "content": "This is a root Comment", "id": 2342246, "parentID": nu

我有Json我正在接收和解析Ok,但我现在有点迷失在如何处理它

"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请求接收根段

CommentSegment fetchedCommentList = new CommentSegment(api);
//this should contain all the info
然后我想我需要一个评论列表

List<Comment> rootComments = new ArrayList<>;
for(Comment comment : fetchedCommentList.getComments()){
    rootComments.add(new Comment(comment));
}
///This should get me the first level, but how to I get the additional 
// levels and get it ordered correctly?
List rootComments=newarraylist;
for(注释注释:fetchedCommentList.getComments()){
添加(新注释(注释));
}
///这应该让我达到第一个层次,但是我如何才能获得额外的层次呢
//级别并正确排序?

现在我的大脑爆炸了,我该怎么办?

你可以使用一个队列,然后沿着数据树往下走,就像这样:

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;
}
私有列表树(段父级){
列表注释=新列表();
Queue Queue=new LinkedList();
for(注释注释:parent.getcomments()){
添加(注释);
而(!queue.isEmpty()){
注释节点=(注释)队列。删除();
注释。添加(注释);
如果(node.childCount>0){
对于(注释子节点:((段)节点.getchildren()).getcomments()){
添加(子级);
}
}
}
}
返回评论;
}

您可以使用队列,然后沿着数据树向下走,如下所示:

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;
}
私有列表树(段父级){
列表注释=新列表();
Queue Queue=new LinkedList();
for(注释注释:parent.getcomments()){
添加(注释);
而(!queue.isEmpty()){
注释节点=(注释)队列。删除();
注释。添加(注释);
如果(node.childCount>0){
对于(注释子节点:((段)节点.getchildren()).getcomments()){
添加(子级);
}
}
}
}
返回评论;
}

>现在我的大脑爆炸了,我该怎么办呃,我想?递归可能是最简单的解决方案。@gregschlom我这样做了,他们说,“我们帮不了你,试试堆栈溢出。”@user234461对,我在考虑我的循环,但我如何进入下一个级别并再次运行它?>现在我的大脑爆炸了,我该从这里走到哪里呃,我想?递归可能是最简单的解决方案。@gregschlom我这样做了,他们说,“我们帮不了你,试试堆栈溢出。”@user234461对,我在考虑我的循环,但我如何进入下一个级别并再次运行它呢?好的,我正在尝试。我让它工作起来了,非常感谢。现在我只需要让它们在Ui中交错出现。好的,我正在尝试让它工作起来。非常感谢。现在我只需要让它们在Ui中交错出现。