java树形菜单对象生成
程序员文章站
2022-07-06 09:08:12
本文实例为大家分享了java树形菜单对象生成的具体代码,供大家参考,具体内容如下1、treenode对象package com.ruoyi.system.utils.vo;import java.ut...
本文实例为大家分享了java树形菜单对象生成的具体代码,供大家参考,具体内容如下
1、treenode对象
package com.ruoyi.system.utils.vo; import java.util.arraylist; import java.util.list; public class treenode { /** * 节点id */ private integer id; /** * 节点名称 */ private string name; /** * 父节点id */ private integer parentid; /** * 子节点列表 */ private list childrenlist = new arraylist(); public integer getid() { return id; } public void setid(integer id) { this.id=id; } public string getname() { return name; } public void setname(string name) { this.name=name; } public integer getparentid() { return parentid; } public void setparentid(integer parentid) { this.parentid=parentid; } public list getchildrenlist() { return childrenlist; } public void setchildrenlist(list childrenlist) { this.childrenlist=childrenlist; } }
2、treeutil
package com.ruoyi.system.utils; import com.ruoyi.system.utils.vo.treenode; import java.util.arraylist; import java.util.list; public class treeutil { private list<treenode> treenodelist =new arraylist<>(); public treeutil(list<treenode> list){ treenodelist = list; } /** * 获取节点信息 * @param nodeid 节点id * @return */ public treenode getnodebyid(int nodeid){ treenode treenode = new treenode(); for (treenode item : treenodelist) { if (item.getid() == nodeid) { treenode = item; break; } } return treenode; } /** * 获取子节点信息 * @param nodeid 节点id * @return */ public list<treenode> getchildrennodebyid(int nodeid){ list<treenode> childrentreenode = new arraylist<treenode>(); for (treenode item : treenodelist) { if(item.getparentid() == nodeid){ childrentreenode.add(item); } } return childrentreenode; } /** * 递归生成tree结构数据 * @param rootid 根节点id * @return */ public treenode generatetreenode(int rootid){ treenode root = this.getnodebyid(rootid); list<treenode> childrentreenode = this.getchildrennodebyid(rootid); for (treenode item : childrentreenode) { treenode node = this.generatetreenode(item.getid()); root.getchildrenlist().add(node); } return root; } public static void main(string[] args) { list<treenode> nodelist = new arraylist<>(); treenode treenode = new treenode(); treenode.setid(1); treenode.setname("中国"); treenode.setparentid(0); nodelist.add(treenode); treenode = new treenode(); treenode.setid(2); treenode.setname("北京市"); treenode.setparentid(1); nodelist.add(treenode); treenode = new treenode(); treenode.setid(3); treenode.setname("浙江省"); treenode.setparentid(1); nodelist.add(treenode); treenode = new treenode(); treenode.setid(4); treenode.setname("杭州市"); treenode.setparentid(3); nodelist.add(treenode); treenode = new treenode(); treenode.setid(5); treenode.setname("西湖区"); treenode.setparentid(4); nodelist.add(treenode); treenode = new treenode(); treenode.setid(6); treenode.setname("滨江区"); treenode.setparentid(4); nodelist.add(treenode); treenode = new treenode(); treenode.setid(7); treenode.setname("金华市"); treenode.setparentid(3); nodelist.add(treenode); treeutil treeutil = new treeutil(nodelist); treenode = treeutil.generatetreenode(1); system.out.println("树状结构json格式数据:" + jsonutils.objecttojson(treenode, 1)); } }
3、jsonutil
package com.ruoyi.system.utils; import com.fasterxml.jackson.core.jsonprocessingexception; import com.fasterxml.jackson.databind.javatype; import com.fasterxml.jackson.databind.objectmapper; import java.util.list; public class jsonutils { // 定义jackson对象 private static final objectmapper mapper = new objectmapper(); /** * 将对象转换成json字符串 * @param obj 需转换的对象 * @param flag json格式化标识(0-否,1-是) * @return */ public static <t> string objecttojson(t obj, int flag) { if (obj == null) { return null; } try { string string; if (flag == 1) { string = obj instanceof string ? (string) obj : mapper.writerwithdefaultprettyprinter().writevalueasstring(obj); } else { string = obj instanceof string ? (string) obj : mapper.writevalueasstring(obj); } return string; } catch (jsonprocessingexception e) { e.printstacktrace(); } return null; } /** * 将json结果集转化为对象 * * @param jsondata json数据 * @param beantype 对象中的object类型 * @return */ public static <t> t jsontopojo(string jsondata, class<t> beantype) { try { t t = mapper.readvalue(jsondata, beantype); return t; } catch (exception e) { e.printstacktrace(); } return null; } /** * 将json数据转换成pojo对象list * <p>title: jsontolist</p> * <p>description: </p> * @param jsondata * @param beantype * @return */ public static <t>list<t> jsontolist(string jsondata, class<t> beantype) { javatype javatype = mapper.gettypefactory().constructparametrictype(list.class, beantype); try { list<t> list = mapper.readvalue(jsondata, javatype); return list; } catch (exception e) { e.printstacktrace(); } return null; } public static void main(string[] args) { system.out.println("测试字符串对象:" + objecttojson("hello world", 1)); } }
4、运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 宝宝感冒了吃什么食物好 治疗感冒食物推荐
下一篇: 妈妈经验贴:预防流感日常习惯最重要
推荐阅读
-
java递归菜单树转换成pojo对象
-
java递归菜单树转换成pojo对象
-
asp.net TreeView递归循环子节点生成树形菜单实例
-
Java实现生成Excel树形表头完整代码示例
-
java编程两种树形菜单结构的转换代码
-
asp.net TreeView递归循环子节点生成树形菜单实例
-
JavaScript递归算法生成树形菜单
-
extjs4.2点击树形菜单生成tab页并访问发送请求
-
extjs4.2点击树形菜单生成tab页并访问发送请求
-
Java 设计一个Hero二叉树,HeroNode. 可以向这个英雄二叉树插入不同的Hero对象,并且按照Hero的血量升排序。 随机生成10个Hero对象,每个Hero对象都有不同的血量值,插