如何使用java程序制作Json的分层树结构

时间:2013-10-27 14:08:07

标签: java json

我必须创建一个Json,它将构成一个像树一样的结构。我已经编写了一个java代码,它正在形成一个类似的结构,但它不能满足目的。这就是我到目前为止所做的...这是我的java class ..

import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GsonProgram {

public static void main(String... args) throws Exception {
     String arr[] = {"Culture", "Salary", "Work", "Effort"};
    EntryListContainer entryListContainer = new EntryListContainer();
    List<Entry> entryList1 = new ArrayList<>();

    for (int i = 0; i < arr.length; i++) {
        Entry entry1 = new Entry();
        Entry entry2 = new Entry();
        entry1.setChildren(arr[i]);
        entryList1.add(entry1);
        entryList2.add(entry2);

   entryListContainer.setEntryList1(entryList1);
   entryListContainer.setEntryList1(entryList2);

    /*Root node this will collapse and get back to Original position on click*/
    entryListContainer.setName("Employee");

    entryListContainer.setName("manager");
    }
    Map<String, String> mapping = new HashMap<>();
    mapping.put("entryList1", "name");
    mapping.put("entryList2", "name");
    Gson gson = new GsonBuilder().serializeNulls().setFieldNamingStrategy(new   DynamicFieldNamingStrategy(mapping)).create();
    System.out.println(gson.toJson(entryListContainer));
}

}     class DynamicFieldNamingStrategy实现了FieldNamingStrategy {

private Map<String, String> mapping;

public DynamicFieldNamingStrategy(Map<String, String> mapping) {
    this.mapping = mapping;
}

@Override
public String translateName(Field field) {
    String newName = mapping.get(field.getName());
    if (newName != null) {
        return newName;
    }

    return field.getName();
       }
   }

   class EntryListContainer {

private List<Entry> entryList1;

public void setEntryList1(List<Entry> entryList1) {
    this.entryList1 = entryList1;
}

public List<Entry> getEntryList1() {
    return entryList1;
    }
 }

class Entry {

private String name;

public String getChildren() {
    return name;
}

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

这是生成的json

{
"name":[{
    "name":"Salary"
},{
    "name":"Salary"
},{
    "name":"Work"
},{
    "name":"Doller"
}]
}

但我想要这个结构......

{
"name":"Employee",
"children":[{
    "name":"Salary"
},{
    "name":"Salary"
},{
    "name":"Work"
},{
    "name":"Doller"
}]
}

我需要这种格式的json.somebody请帮助......

1 个答案:

答案 0 :(得分:0)

伪类结构应该是:

class DynamicFieldNamingStrategy{
   private String name;
   private List<Entry> children;

}

<强> GsonProgram

public class GsonProgram {

    public static void main(String... args) throws Exception {
        Entry entry1 = new Entry();
        entry1.setChildren("Salary");
        Entry entry2 = new Entry();
        entry2.setChildren("Salary");
        Entry entry3 = new Entry();
        entry3.setChildren("Work");
        Entry entry4 = new Entry();
        entry4.setChildren("Doller");
        EntryListContainer entryListContainer = new EntryListContainer();
        ArrayList<Entry> entryList1 = new ArrayList<Entry>();


        entryList1.add(entry1);
        entryList1.add(entry2);
        entryList1.add(entry3);
        entryList1.add(entry4);

        entryListContainer.setEntryList1(entryList1);
        entryListContainer.setName("Employee");

        Map<String, String> mapping = new HashMap<String, String>();
        mapping.put("entryList1", "name");



      Gson gson = new GsonBuilder().serializeNulls().setFieldNamingStrategy(new    DynamicFieldNamingStrategy(mapping)).create();
        System.out.println(gson.toJson(entryListContainer));
    }
}

<强> EntryListContainer

class EntryListContainer {

    private List<Entry> children;
    private String name;

    public String getName() {
        return name;
    }

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

    public void setEntryList1(List<Entry> entryList1) {
        this.children = entryList1;
    }

    public List<Entry> getEntryList1() {
        return children;
    }
}

输出:

{
    "children": [
        {
            "name": "Salary"
        },
        {
            "name": "Salary"
        },
        {
            "name": "Work"
        },
        {
            "name": "Doller"
        }
    ],
    "name": "Employee"
}