- 一个实体类,其中有parentid,通过parentid来展现出无限层级的关系
@Data
@TableName(value = "t_sb_menu")
public class TSbMenu implements Serializable
{
@TableId(value = "id", type = IdType.AUTO)
@ApiModelProperty(value="")
private Integer id;
@TableField(value = "menuName")
@ApiModelProperty(value="")
private String menuname;
/**
* 0:顶级菜单
其它:菜单主键
目前仅两级菜单
*/
@TableField(value = "parentId")
@ApiModelProperty(value="0:顶级菜单, 其它:菜单主键, , 目前仅两级菜单")
private Integer parentid;
@TableField(value = "requestUrl")
@ApiModelProperty(value = "")
private String requesturl;
@TableField(value = "imageUrl")
@ApiModelProperty(value="")
private String imageurl;
@TableField(value = "sort")
@ApiModelProperty(value = "")
private Integer sort;
@TableField(value = "status")
@ApiModelProperty(value="")
private Byte status;
private static final long serialVersionUID = 1L;
public static final String COL_ID = "id";
public static final String COL_MENUNAME = "menuName";
public static final String COL_PARENTID = "parentId";
/**
* 小写的父节点
*/
public static final String COL_PARENTID_LOWE = "parentid";
public static final String COL_REQUESTURL = "requestUrl";
public static final String COL_IMAGEURL = "imageUrl";
public static final String COL_SORT = "sort";
public static final String COL_STATUS = "status";
}
![](https://i-blog.csdnimg.cn/blog_migrate/66c5882643146382dbe9b80443649561.png)
/**
* 将指定的list转换成为树结构的数据
*
* @param list 集合对象,每个对象都有用来表示上下级关系的属性结构
* @param id 集合中用来表示id列
* @param parentid 集合对象中用来表示parentId的列
* @param childs 父子级关系表现出来时,将会在父级节点内添加本属性数组集合,并将自己点放置到本数组集合内
* @param parentId 最顶级的id
* @return 树结构的 JSONArray 对象
*/
public static JSONArray parseTree(List<?> list, String id, String parentid, String childs, Object parentId)
{
JSONArray obj = new JSONArray();
for (Object object : list)
{
JSONObject tmepOjb = JSONObject.parseObject(JSON.toJSONString(object));
Object menuId = tmepOjb.get(id);
Object pid = tmepOjb.get(parentid);
if (parentId.toString().equals(pid.toString()))
{
JSONArray c_node = parseTree(list, id, parentid, childs, menuId);
tmepOjb.put(childs, c_node);
obj.add(tmepOjb);
}
}
return obj;
}
// 菜单转换成为树形结构
JSONArray array = CommonUtil.parseTree(menus, TSbMenu.COL_ID, TSbMenu.COL_PARENTID_LOWE, TSbMenuVo.COL_CHILDS, Constants.ZERO);
![](https://i-blog.csdnimg.cn/blog_migrate/868bff8a0a388cb5310450a33cb58925.png)
- 上面已经成功的拿到了一个JSONArray的树形结构对象了,但我们还可以将这个属性结构对象转换成为java实体类,实体类里面有个childs,泛型为自己,如下
@Data
public class TSbMenuVo implements Serializable
{
private Integer id;
@ApiModelProperty(value = "菜单名")
private String menuname;
/**
* 0:顶级菜单
* 其它:菜单主键
* <p>
* 目前仅两级菜单
*/
@ApiModelProperty(value = "0:顶级菜单, 其它:菜单主键, , 目前仅两级菜单")
private Integer parentid;
@ApiModelProperty(value = "菜单的请求地址")
private String requesturl;
@ApiModelProperty(value = "菜单image路径")
private String imageurl;
@TableField(value = "sort")
@ApiModelProperty(value = "排序")
private Integer sort;
/**
* 菜单的子菜单
*/
private List<TSbMenuVo> childs;
@ApiModelProperty(value = "权限集合")
private List<TSbPurview> purviews;
public static final String COL_CHILDS = "childs";
- JSONArray转换成为java无限层级代码,使用fastjson
List<TSbMenuVo> menuVos = JSONArray.parseArray(array.toJSONString(), TSbMenuVo.class);
![](https://i-blog.csdnimg.cn/blog_migrate/19b3af5f271c47a2a49cf3751f85e5f6.png)