0

我是 json 新手。我能够从 servlet 创建一个 json。但我一定会创建一个像下面这样的 json -

{
"name":"Employee",
"children":[{
    "name":"Subho"
},
{
    "name":"jeet",
    "children":[{
        "name":"rahul"
    },
    {
        "name":"abhijit"
    }]

}]
}

但我创造的就像——

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

我的 servlet 代码是-

 public class ActionServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        Random r = new Random();
        int low = 0;
        int high = 5;
        int R = r.nextInt(high - low) + low;
        /*Sample data for child nodes actual data will be called here*/
        String arr[] = {"Culture", "Salary", "Work", "Economy"};
        /*Responsible for creation of the child nodes and their names */
        Map<String, String> mapping = new HashMap<String, String>();
        EntryListContainer entryListContainer = new EntryListContainer();


            List<Entry> entryList1 = new ArrayList<Entry>();
            for (int i = 0; i < R; i++) {
                /*Model object for the Link*/
                Entry entry1 = new Entry();
                entry1.setChildren(arr[i]);
                entryList1.add(entry1);
            }

            entryListContainer.setEntryList1(entryList1);
            /*Root node this will collapse and get back to Original position on click*/
            entryListContainer.setName("Employee");
            mapping.put("entryList1","name");


        Gson gson = new GsonBuilder().serializeNulls().setFieldNamingStrategy(new DynamicFieldNamingStrategy(mapping)).create();

        System.out.println(gson.toJson(entryListContainer));

        String json = null;
        /*conversion of the json from the generated java object*/
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        json = new Gson().toJson(gson);
        System.out.println(json);
        response.getWriter().write(gson.toJson(entryListContainer));

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        out.close();
    }
}

这是 EntryListContainer 类

public 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;
}

这是 DynamicFieldNamingStrategy 类

public class DynamicFieldNamingStrategy implements 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();
}

此 servlet 代码正在创建一个 json。在这里,我首先创建所有子节点并将它们放在一个列表中(这里是 entryList1),然后将它们放在一个哈希图中。但是我创造的并没有满足我的要求..

请任何人帮我解决这个问题..

4

1 回答 1

2

如果我们将您的 JSon 放入jsoneditoronline,我们会得到:

{
    "name": "Employee",
    "children": [
        {
            "name": "Subho"
        },
        {
            "name": "jeet",
            "children": [
                {
                    "name": "rahul"
                },
                {
                    "name": "abhijit"
                }
            ]
        }
    ]
}

现在我们可以看到每个节点都有其他节点的名称和列表:

节点

public class Node {
    private String name = "";
    private List<Node> children;

    public List<Node> getChildren() {
        return children;
    }

    public void setChildren(List<Node> children) {
        this.children = children;
    }

    public String getName() {
        return name;
    }

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

代码

    Node child  = new Node();       
    child.setName("Employee");

    List<Node> list = new ArrayList<Node>();
    Node subChild  = new Node();        
    subChild.setName("Subho");
    list.add(subChild);

    subChild  = new Node();     
    subChild.setName("jeet");



    List<Node> sublist = new ArrayList<Node>();
    Node subsubChild  = new Node();     
    subsubChild.setName("Subho");
    sublist.add(subsubChild);
    subsubChild  = new Node();      
    subsubChild.setName("Subho");
    sublist.add(subsubChild);


    subChild.setChildren(sublist);
    list.add(subChild);

    child.setChildren(list);

    Gson gson = new Gson();

    String output = gson.toJson(child);

输出:

{"name":"Employee","children":[{"name":"Subho"},{"name":"jeet","children":[{"name":"Subho"},{"name":"Subho"}]}]}
于 2013-11-01T06:57:06.280 回答