Java-java 树 list data 转 nested data

发布于 2017-07-25 21:30:39 字数 514 浏览 1285 评论 1

源数据:

[
{
"id": "0008",
"text": "全省"
},
{
"id": "000800",
"parentId": "0008",
"text": "省本级"
},
{
"id": "000801",
"parentId": "0008",
"text": "哈尔滨市"
},
{
"id": "00080100",
"parentId": "000801",
"text": "市本级"
},
{
"id": "00080101",
"parentId": "000801",
"text": "尚志市"
}
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛泛之交 2017-10-25 08:56:47
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;
}

}
}

测试结果

{"children":[{"id":"000801","parentId":"0008","text":"省本级"},
{"children":[{"id":"00080201","parentId":"000802","text":"通州区"},
{"id":"00080202","parentId":"000802","text":"海淀区"}],
"id":"000802","parentId":"0008","text":"北京市"}],"id":"0008","text":"全省"}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文