java 樹形結構轉換為json字串
package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TestTree {
public static void main(String args[]){
List dList=new ArrayList() ;
HashMap node1 = new HashMap();
node1.put("NodeId", "1000" );
node1.put("NodeName","中國" );
node1.put("ParentId","");
HashMap node2 = new HashMap();
node2.put("NodeId", "1100" );
node2.put("NodeName","河南" );
node2.put("ParentId","1000");
HashMap node21 = new HashMap();
node21.put("NodeId", "1200" );
node21.put("NodeName","北京" );
node21.put("ParentId","1000");
HashMap node211 = new HashMap();
node211.put("NodeId", "1300" );
node211.put("NodeName","河北" );
node211.put("ParentId","1000");
HashMap node3 = new HashMap();
node3.put("NodeId", "1110" );
node3.put("NodeName","駐馬店" );
node3.put("ParentId","1100");
HashMap node31 = new HashMap();
node31.put("NodeId", "1210" );
node31.put("NodeName","朝陽區" );
node31.put("ParentId","1200");
HashMap node311 = new HashMap();
node311.put("NodeId", "1310" );
node311.put("NodeName","張家口市" );
node311.put("ParentId","1300");
HashMap node4 = new HashMap();
node4.put("NodeId", "1111" );
node4.put("NodeName","正陽" );
node4.put("ParentId","1110");
HashMap node41 = new HashMap();
node41.put("NodeId", "1112" );
node41.put("NodeName","北京***" );
node41.put("ParentId","1210");
HashMap node411 = new HashMap();
node411.put("NodeId", "1113" );
node411.put("NodeName","橋西區" );
node411.put("ParentId","1310");
dList.add(node1) ;
dList.add(node2) ;
dList.add(node3) ;
dList.add(node4) ;
dList.add(node21) ;
dList.add(node31) ;
dList.add(node41) ;
dList.add(node211) ;
dList.add(node311) ;
dList.add(node411) ;
HashMap nodeList = new HashMap();
Node root = null;
for (int i=0 ;i<dList.size();i++){
Map dataRecord = (Map) dList.get(i);
Node node = new Node();
node.NodeId= (String) dataRecord.get("NodeId");
node.NodeName = (String) dataRecord.get("NodeName");
node.ParentId = (String) dataRecord.get("ParentId");
nodeList.put(node.NodeId, node);
System.out.print("NodeName:"+node.NodeName+" ");
System.out.print("ParentId:"+node.ParentId+" ");
System.out.println("NodeId:"+node.NodeId);
}
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 {
if(node!=null){
nodeList.get(node.ParentId) ;
((Node) nodeList.get(node.ParentId)).addChild(node);
}
}
}
//**************************************************************************************
System.out.println(root.toString());
}
}
/*
*結點資料結構
* */
class Node{
public String NodeName ;
public String NodeId ;
public String ParentId ;
public Children children =new Children() ;
public String getNodeName() {
return NodeName;
}
public void addChild(Node node) {
this.children.addChildren(node);
}
public void setNodeName(String nodeName) {
NodeName = nodeName;
}
public String getNodeId() {
return NodeId;
}
public void setNodeId(String nodeId) {
NodeId = nodeId;
}
public String getParentId() {
return ParentId;
}
public void setParentId(String parentId) {
ParentId = parentId;
}
public Children getChildren() {
return children;
}
public void setChildren(Children children) {
this.children = children;
}
public String toString(){
String result = "{"
+ "China : '" + NodeName + "'";
for(int i=0 ;i<children.list.size();i++){
if (children.list.get(i)!=null) {
result += ", city: " + children.list.get(i).toString();
}
}
return result + "}";
}
}
/*
* 孩子樹形結構
*/
class Children{
public List<Node> list=new ArrayList<Node>() ;
public List<Node> getList() {
return list;
}
public void setList(List<Node> list) {
this.list = list;
}
public void addChildren(Node node){
list.add(node) ;
}
public String toString(){
String result = "{" ;
for(int i=0 ;i<list.size();i++){
result+= "county : '" + list.get(i).NodeName + "' , ";
list.get(i).children.toString() ;
}
return result + "}";
}
}