使用java创建分层json

时间:2013-11-22 07:26:59

标签: java json

我要求制作如下的json -

{
"name":"Root",
"children":[{
        "name":"Mind",
        "children":[
            {
                "name":"Sea"
            },{
                "name":"water"
            },{
                "name":"Wind"
            }]
    },{
        "name":"Habbit",
        "children":[
            {"name":"Sea"},
            {"name":"water"},
            {"name":"Wind"}
        ]},{
            "name":"Culture",
            "children":[
                {
                    "name":"Sea"
                },{
                    "name":"water"
                },{
                    "name":"Wind"
                }
            ]}               
        ]
 }

但我无法创造它。我创造的是这样的 -

{"name":"Root","children":[{"name":"Mind","children":[{"name":"Sea"},{"name":"water"},{"name":"Wind"}]}]}
{"name":"Root","children":[{"name":"Habbit","children":[{"name":"Sea"},{"name":"water"},{"name":"Wind"}]}]}
{"name":"Root","children":[{"name":"Culture","children":[{"name":"Sea"},{"name":"water"},{"name":"Wind"}]}]}

我在下面给出了我的代码 -

public class Hirarchy {
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) {
        List<String> listofParent = new ArrayList<String>();
        listofParent.add("Mind");
        listofParent.add("Habbit");
        listofParent.add("Culture");

        List<String> listofChild = new ArrayList<String>();
        listofChild.add("Sea");
        listofChild.add("water");
        listofChild.add("Wind");
        Entry mainRoot=null;
        for (int i = 0; i < listofParent.size(); i++) {
            Entry root = new Entry(listofParent.get(i));
            mainRoot= aMethod2form(root, listofChild);
            Entry e=new Entry("Root");
            e.add(mainRoot);
            Gson g=new Gson();
            System.out.println(g.toJson(e));
        }
    }
    private static Entry aMethod2form(Entry root, List<String> listofChild) {
        for(int i=0;i<listofChild.size();i++){
            root.add(new Entry(listofChild.get(i)));
        }
          return root;
    }
}
}

在这段代码中,我为主要父节点取一个String,为子节点和其他父节点取两个列表。现在我在其resp中添加父节点和子节点。列出,并使它们都成为主父节点的子节点。但我无法做到这一点。请帮助任何人..

3 个答案:

答案 0 :(得分:0)

用以下代码替换你的gson行:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(g.toJson(e));

答案 1 :(得分:0)

首先,你实际上没有一个顶级杠杆对象。其次,你不应该把System.out.println(...)放在for循环中。您应该非常清楚自己构建的层次结构。

public class Hirarchy {
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) {
        List<String> listofParent = new ArrayList<String>();
        listofParent.add("Mind");
        listofParent.add("Habbit");
        listofParent.add("Culture");

        List<String> listofChild = new ArrayList<String>();
        listofChild.add("Sea");
        listofChild.add("water");
        listofChild.add("Wind");
        Entry topLevelEntry = new Entry("Root");
        //Entry mainRoot=null;//deleted
        for (int i = 0; i < listofParent.size(); i++) {
            Entry root = new Entry(listofParent.get(i));
            Entry mainRoot= aMethod2form(root, listofChild); //modified
            Entry e=new Entry("Root");
            e.add(mainRoot);
           // Gson g=new Gson();
           // System.out.println(g.toJson(e));
            topLevelEntry.add(e);
        }
        Gson g = new Gson();
        System.out.println(g.toJson(topLevelEntry));
    }
    private static Entry aMethod2form(Entry root, List<String> listofChild) {
        for(int i=0;i<listofChild.size();i++){
            root.add(new Entry(listofChild.get(i)));
        }
          return root;
    }
}
}

答案 2 :(得分:-1)

private JSONObject SendBooking() {
    // TODO Auto-generated method stub
    JSONObject jobj = new JSONObject();

         try {  
                    jobj.put("Status", "1");    
                    jobj.put("CustomerID", "1");                        
                    jobj.put("Remark", "good");
                    jobj.put("BookingDate", "2013:08:30 05:56:56");
                    jobj.put("DueDate", "2013:09:30 05:56:56");

                    JSONArray bookingProcessArray = new JSONArray();
                    JSONObject bProcess = new JSONObject();
                    bProcess.put("ProcessID", "8");
                    bProcess.put("Price", "10.50");
                    bookingProcessArray.put(bProcess);  


                    JSONArray bookingDetails = new JSONArray();
                    JSONObject bDetails = new JSONObject();
                        bDetails.put("Remark", "r1");
                        bDetails.put("Status", "1");
                        bDetails.put("ItemTypeID", "8");
                        bDetails.put("ItemDescription", "Jeans(DC)");
                        bDetails.put("BookingDetailProcesses", bookingProcessArray);
                        bookingDetails.put(bDetails);       
                   jobj.put("BookingDetails", bookingDetails);

                } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
            return jobj;

    }