将平面列表转换为层次结构列表,然后将其转换为JSON

时间:2015-05-16 19:19:26

标签: java json

我在转换" flat"时遇到了一些麻烦。列表到层次结构列表。

orignal列表只知道父母是谁,但我想转换它以便知道它的孩子是谁,也最好同时对它进行排序,以便它们按照

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级,我想要一些方法无论多深都有效。

    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*

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

请使用以下使用Gson库实现的代码

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"}]}