2
Я пытаюсь конвертировать мои данные, которые проживающие в файле с этим Json Format В настоящее время моей программы может получить первое деревоне в состоянии получить данные в Json
Я не в состоянии получить ребенок, что узлы, Как я могу установить внутренние элементы. Я стартер в 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":[]}}
Надежда кто-то может помочь мне Спасибо заранее.
Спасибо, Стивен. Но нам нужно правильно отобразить данные из файла в эту структуру? –