2012-05-31 4 views
0

Java를 사용하여 JQuery Dynatree에 대한 트리를 출력하려고했지만 실패했습니다.java 형식으로 트리를 출력합니다.

public class Category { 
    String name; 

    Category parentCategory; 

    public Category(String name, Category parentCategory) { 
     super(); 
     this.name = name; 
     this.parentCategory = parentCategory; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Category getParentCategory() { 
     return parentCategory; 
    } 

    public void setParentCategory(Category parentCategory) { 
     this.parentCategory = parentCategory; 
    } 
} 

Main 클래스

import java.util.ArrayList; 
import java.util.List; 

public class Main { 

    static Category root; 
    static Category category1; 
    static Category category11; 
    static Category category12; 
    static Category category2; 
    static Category category21; 
    static Category category22; 

    static Category category3; 

    public static void main(String[] args) { 
     root = new Category("root", null); 

     category1 = new Category("c1", root); 
     category11 = new Category("c11", category1); 
     category12 = new Category("c12", category1); 

     category2 = new Category("c2", root); 
     category21 = new Category("c21", category2); 
     category22 = new Category("c22", category2); 

     category3 = new Category("c3", root); 

     print(root); 
    } 

    public static void print(Category category){  
     List<Category> childList = getChild(category); 

     if(category.getParentCategory() == null){ //root 
      System.out.println("{title:"+category.getName()+",key:"+category.getName()+","); 
     } 
     else { 
      //if(childList.size()==0){ 
      // System.out.println("{title:"+category.getName()+",key:"+category.getName()+","); 
      //} 
     // else { 
       System.out.println("children:[{title:"+category.getName()+",key:"+category.getName()+","); 
     // }   
     }   

     if(childList.size()==0) return; 
     for(Category child : childList){ 
      print(child); 
     } 
     System.out.println("}]"); 
     System.out.println("}"); 
    } 

    public static List<Category> getChild(Category category){ 
     List<Category> childList = new ArrayList<Category>(); 
     if("root".equals(category.getName())){ 
      childList.add(category1); 
      childList.add(category2); 
      childList.add(category3); 

      return childList; 
     } 

     if("c1".equals(category.getName())){ 
      childList.add(category11); 
      childList.add(category12); 

      return childList;   
     } 

     if("c2".equals(category.getName())){ 
      childList.add(category21); 
      childList.add(category22); 

      return childList;   
     } 

     return childList;// empty 

    } 

} 

내 출력이 정확하지 않습니다, 그것은이 있어야한다 :

{title: "root", key: "root", 
     children: [ 
     {title: "c1", activate: true, 
      children: [ 
      {title: "c11", key: "c11" }, 
      {title: "c12", key: "c12" } 
      ] 
     }, 
     {title: "c2", select: true, 
      children: [ 
      {title: "c21", key: "c21" }, 
      {title: "c22", key: "c22" } 
      ] 
     }, 
     {title: "c3", hideCheckbox: true }  
     ] 
    } 

내 코드는 버그가 어떻게 그것을 해결하기 위해? 또는이 디자인을 사용하면이 결과를 얻을 수 있습니까?

감사합니다.

답변

1

출력이 JSON 인 것으로 보입니다.

gson을 사용해 보셨습니까? 데이터를 직렬화하는 코드를 작성할 필요가 없습니다. gson은 객체를 JSON으로 직렬화합니다.

관련 문제