Java 无法获取Json格式的数据

Java 无法获取Json格式的数据,java,json,tree,gson,Java,Json,Tree,Gson,我正在尝试将驻留在文件中的数据转换为此 目前我的程序能够得到第一棵树 我无法获得该节点的子节点,如何设置内部元素。我是Json的初学者 public class id3Json { public static String SPACE = " "; public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Buff

我正在尝试将驻留在文件中的数据转换为此 目前我的程序能够得到第一棵树

我无法获得该节点的子节点,如何设置内部元素。我是Json的初学者

public class id3Json {

    public static String SPACE = " ";

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        BufferedReader bf = new BufferedReader(new FileReader("ruleset.txt"));
        String line = "";
        Gson gson = new Gson();
        Json1 tree = new Json1();
        Json2 attrib = new Json2();
        int itr = 0;
        while ((line = bf.readLine()) != null) {
            System.out.println("\n----------" + line);
            String[] parts = line.split(SPACE);
            if (parts.length != 0 && itr == 0) {
                attrib.setLeaf(false);
                attrib.setAttribute(parts[0]);
            }
            // create an array for children in attrib
            Json3 child = new Json3();
            child.setAttributeIndex(Integer.parseInt(parts[0]));
            child.setAttributeName(parts[1]);
            System.out.println("child: " + gson.toJson(child));
            if(parts.length>0){
                Json2 attrib1 = new Json2();
                Json3[] arr = null;
                List<Json3> grpclsJsonList = new ArrayList<>();
                for(int i=2;i<parts.length;i++){
                    if(i>3 && parts.length>3){

                        attrib1.setAttribute(parts[i]);
                        attrib1.setLeaf(false);
                    }
                    else{

                        attrib1.setAttribute(parts[i]);
                        attrib1.setLeaf(true);
                        arr = grpclsJsonList.toArray(new Json3[0]);
                        attrib1.setChildren(arr);
                    }
                }
                System.out.println("attrib1 ---- "+gson.toJson(attrib1));
                child.setChildren(attrib1);
            }
            System.out.println("childddd------ "+gson.toJson(child));
                itr++;
        }

    }

}

希望有人能帮我
提前感谢。

您不需要手动操作。格森可以帮你。您只需创建适当的类并指出正确的结构:

static class Response {
    public Response(Node tree, double accuracy) {
        this.tree = tree;
        this.accuracy = accuracy;
    }

    Node tree;
    double accuracy;
}

static class Node {
    public Node(String value, List<Node> children) {
        this.value = value;
        this.children = children;
    }

    String value;
    List<Node> children;
}

public static void main(final String[] args) {

    final Node tree = new Node("root", Lists.newArrayList(
            new Node("level1_1",
                    Lists.newArrayList(new Node("level2_1", null), new Node("level2_2", null))),
            new Node ("level1_2",
                    Lists.newArrayList(new Node("level2_3", null), new Node("level2_4", null)))
            ));

    final Response r = new Response(tree, 100.0d);

    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    System.out.println(gson.toJson(r));
}
静态类响应{
公众响应(节点树,双精度){
this.tree=树;
准确度=准确度;
}
节点树;
双精度;
}
静态类节点{
公共节点(字符串值,列表子节点){
这个值=值;
这个。孩子=孩子;
}
字符串值;
列出儿童名单;
}
公共静态void main(最终字符串[]args){
最终节点树=新节点(“根”,Lists.newArrayList(
新节点(“level1_1”,
Lists.newArrayList(新节点(“level2_1”,null),新节点(“level2_2,null)),
新节点(“level1_2”,
Lists.newArrayList(新节点(“level2_3”,null),新节点(“level2_4”,null)))
));
最终响应r=新响应(树,100.0d);
Gson Gson=new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(r));
}

谢谢Stephen。但是我们需要将数据从文件映射到这个结构,对吗?
static class Response {
    public Response(Node tree, double accuracy) {
        this.tree = tree;
        this.accuracy = accuracy;
    }

    Node tree;
    double accuracy;
}

static class Node {
    public Node(String value, List<Node> children) {
        this.value = value;
        this.children = children;
    }

    String value;
    List<Node> children;
}

public static void main(final String[] args) {

    final Node tree = new Node("root", Lists.newArrayList(
            new Node("level1_1",
                    Lists.newArrayList(new Node("level2_1", null), new Node("level2_2", null))),
            new Node ("level1_2",
                    Lists.newArrayList(new Node("level2_3", null), new Node("level2_4", null)))
            ));

    final Response r = new Response(tree, 100.0d);

    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    System.out.println(gson.toJson(r));
}