package com.pkpm.zjk.util;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Collections;
import com.pkpm.zjk.entity.Qhdm;
public class TreeToJson{
public String ToJson(List<Qhdm> list){
List dataList = new ArrayList<Qhdm>();
for(int i=0 ;i<list.size();i++){
HashMap dataRecord = new HashMap();
dataRecord.put("id", list.get(i).getQhdm());
dataRecord.put("text", list.get(i).getQhmc());
dataRecord.put("parentId",list.get(i).getSjdm());
dataList.add(dataRecord) ;
}
HashMap nodeList = new HashMap();
Node root = null;
for (Iterator it = dataList.iterator(); it.hasNext();){
Map dataRecord = (Map) it.next();
Node node = new Node();
node.id = (String) dataRecord.get("id");
node.text = (String) dataRecord.get("text");
node.parentId = (String) dataRecord.get("parentId");
nodeList.put(node.id, node);
}
Set entrySet = nodeList.entrySet();
for (Iterator it = entrySet.iterator(); it.hasNext();) {
Node node = (Node) ((Map.Entry) it.next()).getValue();
if (node.parentId == null || node.parentId.equals("")) {
root = node;
} else {
((Node) nodeList.get(node.parentId)).addChild(node);
}
}
// 输出无序的树形菜单的JSON字符串
// System.out.println(root.toString());
// root.sortChildren();
// 输出有序的树形菜单的JSON字符串
System.out.println(root.toString());
return root.toString() ;
}
}
/**
* 节点类
*/
class Node {
/**
* 节点编号
*/
public String id;
/**
* 节点内容
*/
public String text;
/**
* 父节点编号
*/
public String parentId;
/**
* 孩子节点列表
*/
private Children children = new Children();
// 先序遍历,拼接JSON字符串
public String toString() {
String result = "{"
/* + "id : '" + id + "'"
+ ", text : '" + text + "'";*/
// + "id : '" + id + "'"
+ "name : '" + text + "'";
if (children != null && children.getSize() != 0) {
result += ", cityList: " + children.toString();
}
return result + "}";
}
}
// 兄弟节点横向排序
public void sortChildren() {
if (children != null && children.getSize() != 0) {
children.sortChildren();
}
}
// 添加孩子节点
public void addChild(Node node) {
this.children.addChild(node);
}
}
/**
* 孩子列表类
*/
class Children {
private List list = new ArrayList();
public int getSize() {
return list.size();
}
public void addChild(Node node) {
list.add(node);
}
// 拼接孩子节点的JSON字符串
public String toString() {
String result = "[";
for (Iterator it = list.iterator(); it.hasNext();) {
String parentId =((Node) it.next()).parentId ;
System.out.println(parentId) ;
System.out.println(parentId.substring(parentId.length()-3)) ;
if(parentId.substring(parentId.length()-4).equals("0000")){
result += ((Node) it.next()).toString();
result += ",";
}
else{
System.out.println("进入else") ;
result += ((Node) it.next()).toString1();
result += ",";
}
}
result = result.substring(0, result.length() - 1);
result += "]";
return result;
}
// 孩子节点排序
public void sortChildren() {
// 对本层节点进行排序
// 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器
Collections.sort(list, new NodeIDComparator());
// 对每个节点的下一层节点进行排序
for (Iterator it = list.iterator(); it.hasNext();){
((Node) it.next()).sortChildren();
}
}
}
/**
* 节点比较器
*/
class NodeIDComparator implements Comparator {
// 按照节点编号比较
public int compare(Object o1, Object o2) {
int j1 = Integer.parseInt(((Node)o1).id);
int j2 = Integer.parseInt(((Node)o2).id);
return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
}
}