dhtmlxtree json 형식 한 번에 불러오기

3596 단어 json
보충:linkedList를 사용해야 할 수도 있습니다. 여러 번의 수정이 관련될 수 있으며,ArrayList를 사용하면 ensureCapacity()를 설정하는 것이 좋습니다.
 
public class TreeUtil {
	
	public static String nodeList2Json(String pid,List<TreeNode> list){
		String json="{id:0,"+createTreeNodeJson(pid,list)+"}";
		return json;
	}
	
	private static String createTreeNodeJson(String pid,List<TreeNode> nodelist){
		List<TreeNode> list=_getByPid(pid,nodelist);
		StringBuilder strBulider=new StringBuilder("item:[");
		for(int i=0;i<list.size();i++){
			TreeNode treeNode=list.get(i);
			strBulider.append(node2Json(treeNode));
			if(treeNode.isHasChild()){
				strBulider.append(createTreeNodeJson(treeNode.getId(),nodelist)+"}");
			}
			if(i!=list.size()-1)
				strBulider.append(",");
		}
		strBulider.append("]");
		return strBulider.toString();
	}
	
	
	
	private static String node2Json(TreeNode node){
		StringBuilder strBulider=new StringBuilder("{");
		strBulider.append("id:\"").append(node.getId()).append("\",text:\"").append(node.getText())
			.append("\",im0:\"").append(node.getIm0()).append("\",im1:\"").append(node.getIm1()).append("\",im2:\"").append(node.getIm2()).append("\"");
		if(node.isOpen())
			strBulider.append(",open:1");
		if(node.isCall())
			strBulider.append(",call:1");
		if(node.isSelect())
			strBulider.append(",select:1");
		if(node.isNocheckbox())
			strBulider.append(",nocheckbox:1");
		if(node.isDisabled())
			strBulider.append(",disabled:1");
		if(!StringUtils.isEmpty(node.getUserdata()))
			strBulider.append(",userdata:").append(node.getUserdata());
		if(node.isHasChild())
			strBulider.append(",child:1,");
		else
			strBulider.append(",child:0}");
		return strBulider.toString();
	}
	
	private static List<TreeNode> _getByPid(String pid,List<TreeNode> list){
		List<TreeNode> treeNodeList=new ArrayList<TreeNode>();
		if(StringUtils.isBlank(pid)){
			for(int i=0;i<list.size();i++){
				TreeNode node=list.get(i);
				if(StringUtils.isEmpty(node.getParentId()))
					treeNodeList.add(node);
			}
			return treeNodeList;
		}
		
		for(int i=0;i<list.size();i++){
			TreeNode node=list.get(i);
			if(pid.equals(node.getParentId()))
				treeNodeList.add(node);
		}
		list.removeAll(treeNodeList);
		return treeNodeList;
	}
}

 
 
treeNode 클래스
 
private String id;
	
	private String text;
	
	private String im0;
	
	private String im1;
	
	private String im2;
	
	private int orderNo;
	
	private String userdata;
	
	private boolean isSelect=false; //    
    private boolean isOpen=false; //    
    private boolean isCall=false; //         
    private boolean isChecked=false;//   checked  ,      
    private boolean hasChild = false; //               
    private boolean nocheckbox = false; //        checkbox ,            
    private boolean disabled = false; //            :1   ,0  

	private String parentId;

좋은 웹페이지 즐겨찾기