2013-05-12
展开全部
嗯。之前做的时候写了一个简单的例子,请参考一下。 一个bean类:public class TreeData {
private int id;
private String name;
private int personId; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getPersonId() {
return personId;
} public void setPersonId(int personId) {
this.personId = personId;
}
} action 代码: response.setContentType("text/plain;charset=utf-8");
ArrayList list=new ArrayList();
//根目录(root)
TreeData td=new TreeData();
td.setId(1);
td.setName("一部主管");
td.setPersonId(0);
list.add(td);
TreeData td1=new TreeData();
td1.setId(2);
td1.setName("二部部主管");
td1.setPersonId(0);
list.add(td1);
//一部子节点
TreeData td3=new TreeData();
td3.setId(3);
td3.setName("一部教师1");
td3.setPersonId(1);
list.add(td3);
TreeData td4=new TreeData();
td4.setId(4);
td4.setName("一部教师2");
td4.setPersonId(1);
list.add(td4);
TreeData td5=new TreeData();
td5.setId(5);
td5.setName("一部教师3");
td5.setPersonId(1);
list.add(td5);
//二部子节点
TreeData td6=new TreeData();
td6.setId(6);
td6.setName("二部教师1");
td6.setPersonId(2);
list.add(td6);
TreeData td7=new TreeData();
td7.setId(7);
td7.setName("二部教师2");
td7.setPersonId(2);
list.add(td7);
TreeData td8=new TreeData();
td8.setId(8);
td8.setName("二部教师3");
td8.setPersonId(2);
list.add(td8);
ArrayList lst=new ArrayList();
TreeData tds=null;
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
TreeData dt = (TreeData) iterator.next();
if(dt.getPersonId()>0){
tds=new TreeData();
tds.setId(dt.getId());
tds.setName(dt.getName());
tds.setPersonId(dt.getPersonId());
lst.add(tds);
}
}
StringBuilder json = new StringBuilder();
json.append("[");
for (int i = 0; i < list.size(); i++) {
TreeData tdn=(TreeData)list.get(i);
if(tdn.getPersonId()==0){
json.append("{");
json.append("\"id\":\""+tdn.getId()+"\",\"text\":\""+tdn.getName()+"\",");
json.append("\"children\":[{");
for(int j=0;j<lst.size();j++){
TreeData tdp=(TreeData)lst.get(j);
if(tdp.getPersonId()==tdn.getId()){
json.append("\"id\":\""+tdp.getId()+"\",\"text\":\""+tdp.getName()+"\",");
}
}
json.delete(json.lastIndexOf(","),json.length());
json.append("}]");
json.append("},");
}
}
json.delete(json.lastIndexOf(","),json.length());
json.append("]");
System.out.println(json);
response.getWriter().write(json.toString());
return null;
private int id;
private String name;
private int personId; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getPersonId() {
return personId;
} public void setPersonId(int personId) {
this.personId = personId;
}
} action 代码: response.setContentType("text/plain;charset=utf-8");
ArrayList list=new ArrayList();
//根目录(root)
TreeData td=new TreeData();
td.setId(1);
td.setName("一部主管");
td.setPersonId(0);
list.add(td);
TreeData td1=new TreeData();
td1.setId(2);
td1.setName("二部部主管");
td1.setPersonId(0);
list.add(td1);
//一部子节点
TreeData td3=new TreeData();
td3.setId(3);
td3.setName("一部教师1");
td3.setPersonId(1);
list.add(td3);
TreeData td4=new TreeData();
td4.setId(4);
td4.setName("一部教师2");
td4.setPersonId(1);
list.add(td4);
TreeData td5=new TreeData();
td5.setId(5);
td5.setName("一部教师3");
td5.setPersonId(1);
list.add(td5);
//二部子节点
TreeData td6=new TreeData();
td6.setId(6);
td6.setName("二部教师1");
td6.setPersonId(2);
list.add(td6);
TreeData td7=new TreeData();
td7.setId(7);
td7.setName("二部教师2");
td7.setPersonId(2);
list.add(td7);
TreeData td8=new TreeData();
td8.setId(8);
td8.setName("二部教师3");
td8.setPersonId(2);
list.add(td8);
ArrayList lst=new ArrayList();
TreeData tds=null;
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
TreeData dt = (TreeData) iterator.next();
if(dt.getPersonId()>0){
tds=new TreeData();
tds.setId(dt.getId());
tds.setName(dt.getName());
tds.setPersonId(dt.getPersonId());
lst.add(tds);
}
}
StringBuilder json = new StringBuilder();
json.append("[");
for (int i = 0; i < list.size(); i++) {
TreeData tdn=(TreeData)list.get(i);
if(tdn.getPersonId()==0){
json.append("{");
json.append("\"id\":\""+tdn.getId()+"\",\"text\":\""+tdn.getName()+"\",");
json.append("\"children\":[{");
for(int j=0;j<lst.size();j++){
TreeData tdp=(TreeData)lst.get(j);
if(tdp.getPersonId()==tdn.getId()){
json.append("\"id\":\""+tdp.getId()+"\",\"text\":\""+tdp.getName()+"\",");
}
}
json.delete(json.lastIndexOf(","),json.length());
json.append("}]");
json.append("},");
}
}
json.delete(json.lastIndexOf(","),json.length());
json.append("]");
System.out.println(json);
response.getWriter().write(json.toString());
return null;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询