简体   繁体   中英

Not able to get Data in Json

I am trying to convert my data which is residing in file to this Json Format Currently my program is able to get the first tree

I am not able to get the child of that nodes , How can I set the inner elements. I am a starter in 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++;
        }

    }

}

OUTPUT

----------0 Middle-aged yes
child: {"attributeIndex":0,"attributeName":"Middle-aged"}
attrib1 ---- {"attribute":"yes","isLeaf":true,"children":[]}
childddd------ {"attributeIndex":0,"attributeName":"Middle-aged","children":{"attribute":"yes","isLeaf":true,"children":[]}}

----------0 senior 3 excellent no
child: {"attributeIndex":0,"attributeName":"senior"}
attrib1 ---- {"attribute":"no","isLeaf":false,"children":[]}
childddd------ {"attributeIndex":0,"attributeName":"senior","children":{"attribute":"no","isLeaf":false,"children":[]}}

----------0 senior 3 fair yes
child: {"attributeIndex":0,"attributeName":"senior"}
attrib1 ---- {"attribute":"yes","isLeaf":false,"children":[]}
childddd------ {"attributeIndex":0,"attributeName":"senior","children":{"attribute":"yes","isLeaf":false,"children":[]}}

----------0 youth 2 no no
child: {"attributeIndex":0,"attributeName":"youth"}
attrib1 ---- {"attribute":"no","isLeaf":false,"children":[]}
childddd------ {"attributeIndex":0,"attributeName":"youth","children":{"attribute":"no","isLeaf":false,"children":[]}}

----------0 youth 2 yes yes
child: {"attributeIndex":0,"attributeName":"youth"}
attrib1 ---- {"attribute":"yes","isLeaf":false,"children":[]}
childddd------ {"attributeIndex":0,"attributeName":"youth","children":{"attribute":"yes","isLeaf":false,"children":[]}}

The POJO classes and input

Hope someone can help me out Thanks in advance.

You don't need to do that manually. Gson can do that for you. You just have to create the proper classes and point out the right structure:

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM