本文介绍了将平面列表转换为层次结构列表,然后将其转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在将平面列表转换为层次结构列表时遇到了一些麻烦。
I'm having some trouble converting a "flat" list to a hierarchy list.
orignal列表只知道父母是谁,但我想转换所以它知道它的孩子是谁,也最好同时对它进行排序,以便按照
The orignal list only knows who the parent is but I would like to convert so that it knows who its children are instead, also preferably sort it at the same time so that they get added to the list in the order of
1.1 Parent
2.2 Child
3.3 Child
4.4 Child
2.5 Child
3.6 Child
1.7 Parent
2.8 Child
这就是我所拥有的。然而,这只适用于低至2级,我想要一些方法无论多深都有效。
This is what I've got atm. This however only works for down to 2 levels, I want some method which works no matter how deep it has to go.
System.out.println("Trying to sort the list so that things get added in the order of 1. parent -> 2. children ");
for(int i = 0; i < arrayListUnsorted.size() ; i++){
if(arrayListUnsorted.get(i).getParent().equals("#")){
arrayListSorted.add(arrayListUnsorted.get(i));
arrayListUnsorted.remove(i);
}
}
for(int a = 0; a < arrayListSorted.size(); a++){
for(int b = 0; b < arrayListUnsorted.size(); b++){
if(Integer.toString(arrayListSorted.get(a).getId()).equals(arrayListUnsorted.get(b).getParent())){
arrayListSorted.add(arrayListUnsorted.get(b));
arrayListUnsorted.remove(b);
}
}
}
型号:
public class TreeBranchModel {
private int id;
private String text;
private String parent;
public TreeBranchModel(int id, String text, String parent) {
super();
this.id = id;
this.text = text;
this.parent = parent;
}
*Getters/Setters*
要转换为的模型:
public class TreeBranchModelHeirachy {
private int id;
private String text;
private ArrayList<TreeBranchModelHeirachy> children;
public TreeBranchModelHeirachy(int id, String text, ArrayList<TreeBranchModelHeirachy> children) {
super();
this.id = id;
this.text = text;
this.children = children;
}
*Getters/Setters*
非常感谢任何帮助。
推荐答案
请使用以下使用Gson库实现的代码
Please use the following code implemented using Gson library
import java.lang.reflect.Field;
import java.util.List;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class JsonConvertor {
private static GsonBuilder gsonBuilder;
private static Gson gson;
private JsonConvertor() {
}
public static Object fromJson(String json, Class clz)
{
gson=new Gson();
return gson.fromJson(json,clz);
}
public static String toJson(Object obj) {
gsonBuilder = new GsonBuilder();
gsonBuilder = gsonBuilder
.addSerializationExclusionStrategy(new CustomIclusionStrategy(
obj.getClass()));
gson = gsonBuilder.create();
String json = gson.toJson(obj);
return json;
}
}
class CustomIclusionStrategy implements ExclusionStrategy {
private Class classToIclude;
private Field[] declaredFields;
private List<FieldAttributes> fields;
public CustomIclusionStrategy(List<FieldAttributes> fields) {
this.fields = fields;
}
public CustomIclusionStrategy(Class classToIclude) {
this.classToIclude = classToIclude;
this.declaredFields=classToIclude.getDeclaredFields();
}
// called only if shouldSkipClass returns false
@Override
public boolean shouldSkipField(FieldAttributes f) {
try {
classToIclude.getSuperclass().getDeclaredField(f.getName());
System.out.println(f.getName());
return true;
} catch (Exception e) {
}
return false;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
// if returns false shouldSkipField will be called,
// otherwise shouldSkipField will not be called
return false;
}
}
import java.util.ArrayList;
import java.util.List;
public class Node {
String id;
String name;
String data;
List<Node> children;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public List<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
public String toJson()
{
return JsonConvertor.toJson(this);
}
public Node(String id, String name, String data) {
super();
this.id = id;
this.name = name;
this.data = data;
}
public Node(String id, String name, String data, List<Node> children) {
super();
this.id = id;
this.name = name;
this.data = data;
this.children = children;
}
public static void main(String[] args) {
List<Node> list=new ArrayList<Node>();
Node n2=new Node("2","n2","d2");
Node n3=new Node("3","n3","d3");
Node n4=new Node("4","n4","d4");
list.add(n2);
list.add(n3);
list.add(n4);
Node n1=new Node("1","n1","d1",list);
System.out.println(n1.toJson());
}
}
这是输出:
{"id":"1","name":"n1","data":"d1","children":[{"id":"2","name":"n2","data":"d2"},{"id":"3","name":"n3","data":"d3"},{"id":"4","name":"n4","data":"d4"}]}
这篇关于将平面列表转换为层次结构列表,然后将其转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!