import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
*
* @author zhangdapeng
* @version 1.0,2014年5月13日
* @since 1.0
*/
public class NodeWorker {
/**
*自身id
*/
private String id;
/**
* 父id
*/
private String pid;
/**
* 节点内容
*/
private Object data;
/**
* 孩子节点列表
*/
private ChildrenWorker children = new ChildrenWorker();
public NodeWorker() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
// 先序遍历,拼接JSON字符串
public String toString() {
Class<? extends Object> clazz = data.getClass();
Field[] field = clazz.getDeclaredFields();
String name = null;
String value = null;
StringBuilder result=new StringBuilder();
for (int i = 0; i < field.length; i++) {
name = field[i].getName();
value = (String) invokeGet(data, name);
if(i!=0){
result.append(",").append('"' + name + '"' + ":" + '"' + value + '"');
}else
result.append("{").append('"' + name + '"' + ":" + '"' + value + '"');
}
if (children != null && children.getSize() != 0) {
result.append(",").append('"').append("c").append('"').append(":").append(children.toString());
} else {
// result.append(",").append('"' + "leaf" + '"' + ":" + '"' + "true" + '"');
}
return result.append("}").toString();
}
/**
* java反射bean的get方法
*
* @param objectClass
* @param fieldName
* @return
*/
@SuppressWarnings("unchecked")
public Method getGetMethod(Class objectClass, String fieldName) {
StringBuffer sb = new StringBuffer();
sb.append("get");
sb.append(fieldName.substring(0, 1).toUpperCase());
sb.append(fieldName.substring(1));
try {
return objectClass.getMethod(sb.toString());
} catch (Exception e) {
}
return null;
}
/**
* 执行get方法
*
* @param o
* 执行对象
* @param fieldName
* 属性
*/
public Object invokeGet(Object o, String fieldName) {
Method method = getGetMethod(o.getClass(), fieldName);
try {
return method.invoke(o, new Object[0]);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 添加孩子节点
public void addChild(NodeWorker node) {
this.children.addChild(node);
}
}
/**
*
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author zhangdapeng
* @version 1.0,2014年1月19日
* @since 1.0
*/
public class ChildrenWorker {
private List<NodeWorker> list = new ArrayList<NodeWorker>();
public int getSize() {
return list.size();
}
public void addChild(NodeWorker node) {
list.add(node);
}
// 拼接孩子节点的JSON字符串
public String toString() {
String result = "[";
for (Iterator<NodeWorker> it = list.iterator(); it.hasNext();) {
result += it.next().toString();
result += ",";
}
result = result.substring(0, result.length() - 1);
result += "]";
return result;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;
/**
* @author zhangdapeng
* @version 1.0,2014年5月13日
* @since 1.0
*/
public class MultiTreeBuilder {
/**
*
*/
public MultiTreeBuilder() {
// TODO Auto-generated constructor stub
}
/**
* 安预定义排列
* @param list
* @return
*/
public String generateJson(List<NodeWorker> list) {
// 节点列表(散列表,用于临时存储节点对象)
HashMap<String, NodeWorker> nodemap = new HashMap<String, NodeWorker>();
// 根节点
NodeWorker root = null;
// 根据结果集构造节点列表(存入散列表)
for (Iterator<NodeWorker> it = list.iterator(); it.hasNext();) {
NodeWorker node = it.next();
nodemap.put(node.getId(), node);
}
// 构造无序的多叉树
Set<Entry<String, NodeWorker>> entrySet = nodemap.entrySet();
for (Iterator<Entry<String, NodeWorker>> it = entrySet.iterator(); it.hasNext();) {
NodeWorker node = it.next().getValue();
if (node.getPid() == null || node.getPid().equals("")) {
root = node;
} else {
nodemap.get(node.getPid()).addChild(node);
}
}
// 输出无序的树形菜单的JSON字符串
return root.toString();
}
public static void main(String[] args) {
// 读取层次数据结果集列表
List<NodeWorker> dataList = new ArrayList<NodeWorker>();
NodeWorker dataNode1 = new NodeWorker();
dataNode1.setId("1030000");
Node2 n2=new Node2();
n2.setText("北京软件解放道支部");
n2.setContent("1030000");
dataNode1.setData(n2);
dataNode1.setPid("100000");
NodeWorker dataNode2 = new NodeWorker();
dataNode2.setId("1200000");
Node2 n1=new Node2();
n1.setText("北京软件三大街支部");
n1.setContent("1200000");
dataNode2.setData(n1);
dataNode2.setPid("100000");
NodeWorker dataNode3 = new NodeWorker();
dataNode3.setId("112100");
Node2 n4=new Node2();
n4.setText("北京软件三大街支部4");
n4.setContent("12000004");
dataNode3.setData(n4);
dataNode3.setPid("1200000");
//
// Node dataNode4 = new Node();
// dataNode4.id="113000";
// dataNode4.text="北京软件开发区支部";
// dataNode4.parentId="110000";
NodeWorker dataNode5 = new NodeWorker();
dataNode5.setId("100000");
Node2 n3=new Node2();
n3.setText("北京软件总部");
n3.setContent("100000");
dataNode5.setData(n3);
dataNode5.setPid("");
// Node dataNode6 = new Node();
// dataNode6.id="110000";
// dataNode6.text="北京支部";
// dataNode6.parentId="100000";
//
// Node dataNode7 = new Node();
// dataNode7.id="111000";
// dataNode7.text="北京软件金光道支部";
// dataNode7.parentId="110000";
// HashMap dataNode123 = new HashMap();
// dataNode123.put("id", "123");
// dataNode123.put("text", "北京软件开发区支部");
// dataNode123.put("parentId", "12");
dataList.add(dataNode1);
dataList.add(dataNode2);
dataList.add(dataNode5);
dataList.add(dataNode3);
MultiTreeBuilder mt = new MultiTreeBuilder();
System.out.println(mt.generateJson(dataList));
}
}
/**
* @author zhangdapeng
* @version 1.0,2014年5月13日
* @since 1.0
*/
public class Node2{
/**
* 节点内容
*/
private String text;
/**
* 节点内容
*/
private String content;
/**
*
*/
public Node2() {
// TODO Auto-generated constructor stub
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
这是返回结果:
{
"text": "北京软件总部",
"content": "100000",
"c": [{
"text": "北京软件三大街支部",
"content": "1200000",
"c": [{
"text": "北京软件三大街支部4",
"content": "12000004"
}]
},
{
"text": "北京软件解放道支部",
"content": "1030000"
}]
}