public class Hello {
public static void main ( String [ ] args) {
List < Menu > menuList= new ArrayList < Menu > ( ) ;
menuList. add ( new Menu ( "GN001D000" , "0" , "不稳定性心绞痛" ) ) ;
menuList. add ( new Menu ( "GN001D100" , "GN001D000" , "权限管理" ) ) ;
menuList. add ( new Menu ( "GN001D110" , "GN001D100" , "密码修改" ) ) ;
menuList. add ( new Menu ( "GN001D120" , "GN001D100" , "新加用户" ) ) ;
menuList. add ( new Menu ( "GN001D200" , "GN001D000" , "系统监控" ) ) ;
menuList. add ( new Menu ( "GN001D210" , "GN001D200" , "在线用户" ) ) ;
MenuTree menuTree = new MenuTree ( menuList) ;
menuList= menuTree. builTree ( ) ;
String jsonOutput= JSON. toJSONString ( menuList) ;
System . out. println ( jsonOutput) ;
}
}
public class Menu {
private String id;
private String parentId;
private String name;
private String text;
private String url;
private String yxbz;
private List < Menu > children;
public Menu ( String id, String parentId, String name) {
this . id= id;
this . parentId= parentId;
this . name= name;
this . text= text;
this . url= url;
this . yxbz= yxbz;
}
public String getId ( ) {
return id;
}
public void setId ( String id) {
this . id = id;
}
public String getParentId ( ) {
return parentId;
}
public void setParentId ( String parentId) {
this . parentId = parentId;
}
public String getText ( ) {
return text;
}
public void setText ( String text) {
this . text = text;
}
public String getUrl ( ) {
return url;
}
public void setUrl ( String url) {
this . url = url;
}
public String getYxbz ( ) {
return yxbz;
}
public void setYxbz ( String yxbz) {
this . yxbz = yxbz;
}
public List < Menu > getChildren ( ) {
return children;
}
public void setChildren ( List < Menu > children) {
this . children = children;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
}
public class MenuTree {
private List < Menu > menuList = new ArrayList < Menu > ( ) ;
public MenuTree ( List < Menu > menuList) {
this . menuList= menuList;
}
public List < Menu > builTree ( ) {
List < Menu > treeMenus = new ArrayList < Menu > ( ) ;
for ( Menu menuNode : getRootNode ( ) ) {
menuNode= buildChilTree ( menuNode) ;
treeMenus. add ( menuNode) ;
}
return treeMenus;
}
private Menu buildChilTree ( Menu pNode) {
List < Menu > chilMenus = new ArrayList < Menu > ( ) ;
for ( Menu menuNode : menuList) {
if ( menuNode. getParentId ( ) . equals ( pNode. getId ( ) ) ) {
chilMenus. add ( buildChilTree ( menuNode) ) ;
}
}
pNode. setChildren ( chilMenus) ;
return pNode;
}
private List < Menu > getRootNode ( ) {
List < Menu > rootMenuLists = new ArrayList < Menu > ( ) ;
for ( Menu menuNode : menuList) {
if ( menuNode. getParentId ( ) . equals ( "0" ) ) {
rootMenuLists. add ( menuNode) ;
}
}
return rootMenuLists;
}
public static void main ( String [ ] args) {
List < Menu > menuList= new ArrayList < Menu > ( ) ;
menuList. add ( new Menu ( "1" , "0" , "早期筛查" ) ) ;
menuList. add ( new Menu ( "2" , "1" , "影响因素分析" ) ) ;
menuList. add ( new Menu ( "3" , "1" , "预测分析" ) ) ;
menuList. add ( new Menu ( "11" , "3" , "预测住院人次分析" ) ) ;
menuList. add ( new Menu ( "12" , "3" , "预测再入院" ) ) ;
MenuTree menuTree = new MenuTree ( menuList) ;
menuList= menuTree. builTree ( ) ;
String jsonOutput= JSON. toJSONString ( menuList) ;
System . out. println ( jsonOutput) ;
}
}