1.我用的model类
public class SourceDirTmp {
private String Id;
private String parentId;
private String nodeCode;
private String nodeName;
private String noteNote;
public SourceDirTmp() {
}
public SourceDirTmp(String id, String nodeCode, String nodeName, String noteNote) {
Id = id;
this.nodeCode = nodeCode;
this.nodeName = nodeName;
this.noteNote = noteNote;
}
public SourceDirTmp(String id, String parentId, String nodeCode, String nodeName, String noteNote) {
Id = id;
this.parentId = parentId;
this.nodeCode = nodeCode;
this.nodeName = nodeName;
this.noteNote = noteNote;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public void setId(String id) {
Id = id;
}
public void setNodeCode(String nodeCode) {
this.nodeCode = nodeCode;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public void setNoteNote(String noteNote) {
this.noteNote = noteNote;
}
public String getId() {
return Id;
}
public String getNodeCode() {
return nodeCode;
}
public String getNodeName() {
return nodeName;
}
public String getNoteNote() {
return noteNote;
}
}
2.下面是递归拼接工具Recursion类
public class Recursion {
public Recursion() {
}
public StringBuffer returnStr = new StringBuffer();
public void recursionFn(List list, SourceDirTmp node){
if(hasChild(list,node)){
returnStr.append("{"+"\""+"id"+"\""+":");
returnStr.append("\""+node.getId()+"\"");
returnStr.append(","+"\""+"parentId"+"\""+":");
returnStr.append("\""+node.getParentId()+"\"");
returnStr.append(","+"\""+"nodeCode"+"\""+":");
returnStr.append("\""+node.getNodeCode()+"\"");
returnStr.append(","+"\""+"nodeName"+"\""+":");
returnStr.append("\""+node.getNodeName()+"\"");
returnStr.append(","+"\""+"nodeNote"+"\""+":");
returnStr.append("\""+node.getNoteNote()+"\"");
returnStr.append(","+"\""+"children"+"\""+":[");
List childList = getChildList(list,node);
Iterator it = childList.iterator();
while(it.hasNext()){
SourceDirTmp n = (SourceDirTmp)it.next();
recursionFn(list,n);
}
returnStr.append("]},");
}else{
returnStr.append("{"+"\""+"id"+"\""+":");
returnStr.append("\""+node.getId()+"\"");
returnStr.append(","+"\""+"parentId"+"\""+":");
returnStr.append("\""+node.getParentId()+"\"");
returnStr.append(","+"\""+"nodeCode"+"\""+":");
returnStr.append("\""+node.getNodeCode()+"\"");
returnStr.append(","+"\""+"nodeName"+"\""+":");
returnStr.append("\""+node.getNodeName()+"\"");
returnStr.append(","+"\""+"nodeNote"+"\""+":");
returnStr.append("\""+node.getNoteNote()+"\"");
returnStr.append(","+"\""+"leaf"+"\""+":"+"\""+"true"+"\""+"},");
}
}
public boolean hasChild(List list, SourceDirTmp node){
return (getChildList(list,node).size()) > 0 ?true:false;
}
public List getChildList(List list, SourceDirTmp node){
List li = new ArrayList();
Iterator it = list.iterator();
while(it.hasNext()){
SourceDirTmp n = (SourceDirTmp)it.next();
if(n.getParentId().equals(node.getId())){
li.add(n);
}
}
return li;
}
public String modifyStr(String returnStr){
return ("["+returnStr+"]").replaceAll(",]", "]");
}
}
3.工具类中需要有两个参数,一个是给定节点id下面所有的节点集合(包括该给定节点,也就是我们后来生成嵌套json的父节点),第二个是参数是给定的节点,也就是指定的根节点。下面是调用示例:
//以下是生成Extjs树形json
Recursion r = new Recursion();
r.recursionFn(listTmp,sourceDirTmp);
System.out.println(r.modifyStr(r.returnStr.toString()));
这里的listTmp就是你自己获得的节点集合(包括根节点),sourceDirTmp就是根节点对象,打印的格式如下所示:
[{"id":"5","parentId":"1","nodeCode":"ITOB","nodeName":"IT对象","nodeNote":"null","children":[{"id":"488","parentId":"5","nodeCode":"APP","nodeName":"应用资源","nodeNote":"null","leaf":"true"},{"id":"491","parentId":"5","nodeCode":"USER","nodeName":"用户","nodeNote":"null","leaf":"true"},{"id":"490","parentId":"5","nodeCode":"DATA","nodeName":"数据资源","nodeNote":"null","leaf":"true"},{"id":"487","parentId":"5","nodeCode":"HDWE","nodeName":"硬件资源","nodeNote":"null","leaf":"true"},{"id":"489","parentId":"5","nodeCode":"SRV","nodeName":"服务资源","nodeNote":"null","leaf":"true"},{"id":"492","parentId":"5","nodeCode":"ROLE","nodeName":"角色","nodeNote":"null","leaf":"true"}]}]