java 树 list data 转 nested data
kimi.
10 years, 3 months ago
Answers
- model AreaTree
@Data
public class AreaTree implements java.io.Serializable{
private static final long serialVersionUID = 9041919159007171464L;
public static String[] excludesProperties = {"parent"};
private String id;
private String text;
private String parentId;
private AreaTree parent;
private List<AreaTree> children;
}
- 测试代码
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.collection.internal.PersistentSet;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class TestAreaTree {
@Test
public void testList(){
List<AreaTree> list = initList();//组织测试数据
AreaTree root = list.get(0);//根节点
for (AreaTree areaTree : list) {//遍历集合
addChild(root,areaTree);
}
System.out.println(toJsonByFilter(root, null, AreaTree.excludesProperties));
}
private void addChild(AreaTree root, AreaTree areaTree) {
if(!areaTree.equals(root)){//不是根节点,开始递归
if(root.getId().equals(areaTree.getParentId())){
List<AreaTree> children = root.getChildren();
if(children == null){
children = new ArrayList<AreaTree>();
}
children.add(areaTree);
root.setChildren(children);
}else{
List<AreaTree> children = root.getChildren();
if(children != null){
for(AreaTree aAreaTree : children){
addChild(aAreaTree,areaTree);
}
root.setChildren(children);
}
}
}
}
private List<AreaTree> initList(){
String[][] arrArr = {{"0008","全省",null},{"000801","省本级","0008"},{"000802","北京市","0008"},{"00080201","通州区","000802"},{"00080202","海淀区","000802"}};
List<AreaTree> list = new ArrayList<AreaTree>();
for(String[] arr : arrArr){
AreaTree areaTree = new AreaTree();
areaTree.setId(arr[0]);
areaTree.setText(arr[1]);
areaTree.setParentId(arr[2]);
list.add(areaTree);
}
return list;
}
/**
* 将对象转换成JSON字符串
* @param object 待转换对象
* @param includesProperties 转换json包含的属性
* @param excludesProperties 转换json排除的属性
* @return json字符串
*/
private String toJsonByFilter(Object object, String[] includesProperties, String[] excludesProperties) {
FastjsonFilter filter = new FastjsonFilter();// excludes优先于includes
if (excludesProperties != null && excludesProperties.length > 0) {
filter.getExcludes().addAll(Arrays.<String> asList(excludesProperties));
}
if (includesProperties != null && includesProperties.length > 0) {
filter.getIncludes().addAll(Arrays.<String> asList(includesProperties));
}
// 使用SerializerFeature.WriteDateUseDateFormat特性来序列化日期格式的类型为yyyy-MM-dd hh24:mi:ss
// 使用SerializerFeature.DisableCircularReferenceDetect特性关闭引用检测和生成
String json = JSON.toJSONString(object, filter, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.DisableCircularReferenceDetect);
return json;
}
class FastjsonFilter implements PropertyFilter {
private final Set<String> includes = new HashSet<String>();
private final Set<String> excludes = new HashSet<String>();
public Set<String> getIncludes() {
return includes;
}
public Set<String> getExcludes() {
return excludes;
}
@Override
public boolean apply(Object source, String name, Object value) {
if (value != null && (value instanceof PersistentSet)) {// 避免hibernate对象循环引用,一切Set属性不予序列化
return false;
}
if (excludes.contains(name)) {
return false;
}
if (excludes.contains(source.getClass().getSimpleName() + "." + name)) {
return false;
}
if (includes.size() == 0 || includes.contains(name)) {
return true;
}
if (includes.contains(source.getClass().getSimpleName() + "." + name)) {
return true;
}
return false;
}
}
}
-
测试结果
卷毛的黑猫
answered 10 years, 3 months ago