2015-03-18 4 views
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":[]}} 

POJO classes and input

Надежда кто-то может помочь мне Спасибо заранее.

ответ

0

Вам не нужно делать это вручную. Гнос может это сделать для вас. Вам просто нужно создать соответствующие классы и указать правильную структуру:

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

Спасибо, Стивен. Но нам нужно правильно отобразить данные из файла в эту структуру? –

Смежные вопросы