Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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
如何";“中断”;一个JSON树在Java中被分解成不同的列表,从父级到每个连接的子级?_Java_Json - Fatal编程技术网

如何";“中断”;一个JSON树在Java中被分解成不同的列表,从父级到每个连接的子级?

如何";“中断”;一个JSON树在Java中被分解成不同的列表,从父级到每个连接的子级?,java,json,Java,Json,我有以下JSON,子对象可以有更多子对象: { "color": "red", "list": [{ "color": "blue", "list": [{ "color": "yellow" }, { "color": "black", "list": [{ "color"

我有以下JSON,子对象可以有更多子对象:

{
    "color": "red",
    "list": [{
            "color": "blue",
            "list": [{
                "color": "yellow"
            }, {
                "color": "black",
                "list": [{
                    "color": "purple"
                }, {
                    "color": "white"
                }]
            }]
        },
        {
            "color": "green",
            "list": [{
                "color": "pink",
                "list": [{
                    "color": "gray"
                }, {
                    "color": "brown"
                }]
            }]
        }
    ]
}
从以下树:

我想将图表分解为从父级到连接的每个子级的单独列表:

LIST1 = red,blue
LIST2 = red,blue,yellow
LIST3 = red,blue,black
LIST4 = red,blue,black,purple
LIST5 = red,blue,black,white
LIST6 = red,green
LIST7 = red,green,pink
LIST8 = red,green,pink,grey
LIST9 = red,green,pink,brown

这是一个有趣的问题,试图解决这个问题。如果解决方案有帮助,请告诉我。我已经用Java实现了这一点

// Java program to print all the node to leaf path    
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/* A binary tree node has data, pointer to left child
       and a pointer to right child */
class Color {

    private String color;
    private List<Color> list;

    public Color() {
        //default
    }

    public Color(String color) {
        this.color = color;
        list = new ArrayList();
    }

    public Color(String color, List<Color> list) {
        this.color = color;
        this.list = list;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void add(Color c) {
        this.list.add(c);
    }

    public void setList(List<Color> list) {
        this.list = list;
    }

    public String getColor() {
        return color;
    }

    public List<Color> getList() {
        return this.list;
    }
}

class ColorTree {
    Color root;

    /*Given a binary tree, print out all of its root-to-leaf
      paths, one per line. Uses a recursive helper to do
      the work.*/
    void printPaths(Color node) {
        String[] path = new String[1000];
        printPathsRecur(node, path, 0);
    }

    /* Recursive helper function -- given a node, and an array
       containing the path from the root node up to but not
       including this node, print out all the root-leaf paths.*/
    void printPathsRecur(Color node, String[] path, int pathLen) {
        if (node == null)
            return;

        /* append this node to the path array */
        path[pathLen] = node.getColor();
        pathLen++;
        printArray(path, pathLen);

        /* it's a leaf, so print the path that led to here  */
        if (node.getList() == null || node.getList().size() == 0) {
            //printArray(path, pathLen);
        } else {
            Iterator<Color> colorIter = node.getList().iterator();
            /* otherwise try subtrees */
            while (colorIter.hasNext()) {
                printPathsRecur(colorIter.next(), path, pathLen);
            }
        }
    }

    /* Utility function that prints out an array on a line. */
    void printArray(String[] names, int len) {
        int i;
        for (i = 0; i < len; i++) {
            System.out.print(names[i] + " ");
        }
        System.out.println();
    }


    private static Color convertJsonToColor(String input) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        return mapper.readValue(input, Color.class);
    }

    // driver program to test above functions
    public static void main(String[] args) throws IOException {

        String jsonInput = "{ \"color\": \"red\", \"list\": [{ \"color\": \"blue\", \"list\": [{ \"color\": \"yellow\" }, { \"color\": \"black\", \"list\": [{ \"color\": \"purple\" }, { \"color\": \"white\" }] }] }, { \"color\": \"green\", \"list\": [{ \"color\": \"pink\", \"list\": [{ \"color\": \"gray\" }, { \"color\": \"brown\" }] }] } ] }";

        ColorTree tree = new ColorTree();
        tree.root = convertJsonToColor(jsonInput);

        /* Let us test the built tree by printing Insorder traversal */
        tree.printPaths(tree.root);
    }
}
//打印所有节点到叶路径的Java程序
导入com.fasterxml.jackson.annotation.JsonInclude;
导入com.fasterxml.jackson.databind.DeserializationFeature;
导入com.fasterxml.jackson.databind.ObjectMapper;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
/*二叉树节点有数据,指针指向左边的子节点
和一个指向正确孩子的指针*/
类颜色{
私有字符串颜色;
私人名单;
公共颜色(){
//违约
}
公共颜色(字符串颜色){
这个颜色=颜色;
列表=新的ArrayList();
}
公共颜色(字符串颜色、列表){
这个颜色=颜色;
this.list=列表;
}
公共void setColor(字符串颜色){
这个颜色=颜色;
}
公共空白添加(c色){
本.列表.添加(c);
}
公共无效集合列表(列表){
this.list=列表;
}
公共字符串getColor(){
返回颜色;
}
公共列表getList(){
返回此.list;
}
}
类颜色树{
色根;
/*给定一棵二叉树,将其所有根到叶打印出来
路径,每行一个。使用递归帮助程序
工作*/
无效打印路径(颜色节点){
字符串[]路径=新字符串[1000];
printPathsRecur(节点,路径,0);
}
/*递归辅助函数——给定一个节点和一个数组
包含从根节点到但不包含的路径
包括此节点,打印出所有根叶路径*/
void printPathsRecur(颜色节点,字符串[]路径,int路径){
if(node==null)
返回;
/*将此节点附加到路径数组*/
path[pathLen]=node.getColor();
pathLen++;
打印阵列(路径,路径);
/*这是一片叶子,所以打印出通向这里的路径*/
if(node.getList()==null | | node.getList().size()==0){
//打印阵列(路径,路径);
}否则{
迭代器colorIter=node.getList().Iterator();
/*否则请尝试子树*/
while(colorIter.hasNext()){
printPathsRecur(colorIter.next(),path,pathLen);
}
}
}
/*在一行上打印出一个数组的实用函数*/
void printary(字符串[]名称,int len){
int i;
对于(i=0;i
使用递归。对于给定的对象和已遍历的颜色列表,将对象的颜色添加到列表中,并使用颜色列表的副本为子列表的每个对象递归。您可以使用任何库(如jackson或gson)在java中读取json,并尝试自己编写代码以生成单独的列表。如果遇到任何问题,请用代码更新问题。您尝试过做什么?显示一些代码非常感谢@Ana,我已经做了几天了,我的自我递归版本远远不是这样。