把list数据转化成easy ui树标签能够识别的json字符串

时间:2023-01-04 10:30:49

首先list的object对象必须是包含父id属性的对象,如本文的function,下面就给大家分享怎样实现。

function类的代码如下:

/**
*
* @ClassName: Function
* @Description: 权限功能树
* @date 2015年9月16日 上午11:07:32
*
*/
public class Function {
private Long id;// 主键;
private Long pId;// 父ID;
private String name;// 功能名称
private String url;// 路径

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getpId() {
return pId;
}

public void setpId(Long pId) {
this.pId = pId;
}

public String getName() {
return name;
}

public Function() {
super();
// TODO Auto-generated constructor stub
}

public void setName(String name) {
this.name = name;
}

public Function(Long id, Long pId, String name, String url) {
super();
this.id = id;
this.pId = pId;
this.name = name;
this.url = url;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}
}
接下来,定义满足要求的树形结构的dto类,如下代码:

import java.util.List;

/**
*
* @ClassName: TreeDto
* @Description: 树形结果dto(满足easy ui树形结构的dto)
* @author yangbo 605051929@qq.com
* @date 2015年9月16日 上午10:57:21
*
*/
public class TreeDto {
private Long id;// id编号
private String text;// 内容
private List<TreeDto> children;// 子节点
private String attribute1;// 扩展内容1
private String attribute2;// 扩展内容2

public TreeDto(Long id, String text, List<TreeDto> children,
String attribute1, String attribute2) {
super();
this.id = id;
this.text = text;
this.children = children;
this.attribute1 = attribute1;
this.attribute2 = attribute2;
}

public TreeDto(Long id, String text, List<TreeDto> children) {
super();
this.id = id;
this.text = text;
this.children = children;
this.attribute1 = "";
this.attribute2 = "";
}

public TreeDto() {
super();
// TODO Auto-generated constructor stub
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public List<TreeDto> getChildren() {
return children;
}

public void setChildren(List<TreeDto> children) {
this.children = children;
}

public String getAttribute1() {
return attribute1;
}

public void setAttribute1(String attribute1) {
this.attribute1 = attribute1;
}

public String getAttribute2() {
return attribute2;
}

public void setAttribute2(String attribute2) {
this.attribute2 = attribute2;
}

@Override
public String toString() {
StringBuffer sb = new StringBuffer("{\"id\":" + id + ", \"text\":\""
+ text + "\",\"attributes\":{\"attribute1\":\"" + attribute1
+ "\",\"attribute2\":\"" + attribute2 + "\"}");

if (children != null && children.size() > 0) {
sb.append(",\"children\":" + children + "");
}

sb.append("}");
return sb.toString();
}
}

接下来,我们定义test类来实现把list的数据转化成满足要求的树形结构字符串,代码如下:

public class Test {
public static void main(String[] argv) {
Function fun = new Function();
fun.setId(0000l);
fun.setName("000000");
fun.setpId(null);
fun.setUrl("URL0");
String results=getAgentTreeData(fun, init());
System.out.print(results);
}
/**
*
* @Description:获取树形格式的字符串
* @param pid
* @return
*
*/
public static String getAgentTreeData(Function function, List<Function> functions) {
TreeDto dtd = new TreeDto();
dtd.setId(function.getId());
dtd.setText(function.getName());
dtd.setAttribute1(function.getUrl());
dtd.setChildren(dtoToTreeDto(function.getId(), functions));
StringBuffer sb = new StringBuffer("[");
sb.append(dtd.toString());
sb.append("]");
String result = sb.toString();
result = result.replaceAll("\\{\\},", "");
return result;
}

/**
*
* @Description: 把dto转化成树形结构Dto
* @param pid
* @param distributions
* @return
*
*/
public static List<TreeDto> dtoToTreeDto(Long pid,
List<Function> functions) {
List<TreeDto> dtds = new ArrayList<TreeDto>();
for (Function entity : functions) {
if (pid.equals(entity.getpId())) {
TreeDto dtd = new TreeDto();
dtd.setId(entity.getId());
dtd.setText(entity.getName());
dtd.setAttribute1(entity.getUrl());
dtd.setChildren(dtoToTreeDto(entity.getId(), functions));
dtds.add(dtd);
}
}
return dtds;
}
<span style="white-space:pre"></span>//定义初始化数据public static List<Function> init() {List<Function> functions = new LinkedList<Function>();Function fun0 = new Function();fun0.setId(00001l);fun0.setName("000000");fun0.setpId(0000l);fun0.setUrl("URL0");functions.add(fun0);Function fun1 = new Function();fun1.setId(111111l);fun1.setName("1111111");fun1.setpId(0000l);fun1.setUrl("URL1");functions.add(fun1);Function fun2 = new Function();fun2.setId(222222l);fun2.setName("222222222222222");fun2.setpId(0000l);fun2.setUrl("URL2");functions.add(fun2);Function fun3 = new Function();fun3.setId(33333l);fun3.setName("333333333333");fun3.setpId(111111l);fun3.setUrl("URL3");functions.add(fun3);Function fun4 = new Function();fun4.setId(4444444444l);fun4.setName("4444444444");fun4.setpId(222222l);fun4.setUrl("URL4");functions.add(fun4);Function fun5 = new Function();fun5.setId(555555555l);fun5.setName("555555555555");fun5.setpId(222222l);fun5.setUrl("URL5");functions.add(fun5);return functions;}}