我正在尝试将驻留在文件中的数据转换为此Json Format 目前我的程序能够获得第一棵树
我无法获得该节点的子节点,如何设置内部元素。我是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++;
}
}
}
输出
----------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":[]}}
希望有人可以帮助我 提前谢谢。
答案 0 :(得分:0)
您无需手动执行此操作。 Gson可以为你做到这一点。您只需要创建适当的类并指出正确的结构:
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));
}