具有树结构的分层JSON

时间:2013-11-20 17:38:03

标签: java json gson

我必须使用Java创建一个类似JSON结构的树,其中我有一个父节点,其中包含多个子节点,依此类推。这是我的代码,我已经部分地完成了这个,但没有完全成功做到这一点。我需要的输出

{
    "name": "Culture", 
    "children": [
        {
            "name": "Salary"
        }, 
        {
            "name": "Work", 
            "children": [
                {
                    "name": "Effort"
                }, 
                {
                    "name": "trust"
                }
            ]
        }
    ]
}

但我正在生成的是

{"name":"Culture",[{"name":"Salary"},{"name":"Work"},{"name":"Effort"}],"name":"Work",[{"name":"Culture"},{"name":"Work"}]}

这是我的代码:

import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;

public class ParentChildApp {
public static class EntryListContainer {

    public List<Entry> children = new ArrayList<Entry>();
    public Entry name;

}

public static class Entry {

    private String name;

    public Entry(String name) {
        this.name = name;
    }

}

public static void main(String[] args) {

    EntryListContainer elc1 = new EntryListContainer();
    elc1.name = new Entry("Culture");
    elc1.children.add(new Entry("Salary"));
    elc1.children.add(new Entry("Work"));
    elc1.children.add(new Entry("Effort"));

    EntryListContainer elc2 = new EntryListContainer();
    elc2.name = new Entry("Work");
    elc2.children.add(new Entry("Culture"));
    elc2.children.add(new Entry("Work"));

    ArrayList<EntryListContainer> al = new ArrayList<EntryListContainer>();
    Gson g = new Gson();

    al.add(elc1);
    al.add(elc2);

    StringBuilder sb = new StringBuilder("{");
    for (EntryListContainer elc : al) {

        sb.append(g.toJson(elc.name).replace("{", "").replace("}", ""));
        sb.append(",");
        sb.append(g.toJson(elc.children));
        sb.append(",");
    }

    String partialJson = sb.toString();

    if (al.size() > 1) {
        int c = partialJson.lastIndexOf(",");
        partialJson = partialJson.substring(0, c);
    }

    String finalJson = partialJson + "}";
    System.out.println(finalJson);

    }

}

1 个答案:

答案 0 :(得分:4)

这样做:

package stackoverflow.questions;

import com.google.gson.*;

import java.util.ArrayList;
import java.util.List;

public class ParentChildApp {

   public static class Entry {

      private String name;

      public Entry(String name) {
         this.name = name;
      }

      private List<Entry> children;

      public void add(Entry node){
         if (children == null)
            children = new ArrayList<Entry>();
         children.add(node);
      }

   }

   public static void main(String[] args) {

      Entry workNode = new Entry("Work");
      workNode.add(new Entry("Effort"));
      workNode.add(new Entry("Trust"));

      Entry salaryNode = new Entry("Salary");


      Entry cultureNode = new Entry("Culture");
      cultureNode.add(salaryNode);
      cultureNode.add(workNode);

      Gson g = new Gson();

      System.out.println(g.toJson(cultureNode));
   }

}

您将获得正在寻找的JSON。