Reputation: 547
I am new to json. I am able to create a json from servlet. But I am bounded to create a json like below-
{
"name":"Employee",
"children":[{
"name":"Subho"
},
{
"name":"jeet",
"children":[{
"name":"rahul"
},
{
"name":"abhijit"
}]
}]
}
But what I create is like-
{
"children":[
{"name":"Culture"},
{"name":"Salary"},
{"name":"Work"},
{"name":"Economy"}
],
"name":"Employee"
}
My servlet code is-
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();
}
}
This is the EntryListContainer class
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;
}
This is the DynamicFieldNamingStrategy class
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();
}
This servlet code is creating a json. Here 1st I create all the children nodes and put them in a list (here entryList1), and then put them in a hashmap. But what I create is not fulfilling my requirement..
Please anyone help me with this..
Upvotes: 0
Views: 1107
Reputation: 77904
If we will put your JSon to jsoneditoronline, we get:
{
"name": "Employee",
"children": [
{
"name": "Subho"
},
{
"name": "jeet",
"children": [
{
"name": "rahul"
},
{
"name": "abhijit"
}
]
}
]
}
Now we can see that each Node has name and list of other Nodes:
Node
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;
}
}
Code
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);
Output:
{"name":"Employee","children":[{"name":"Subho"},{"name":"jeet","children":[{"name":"Subho"},{"name":"Subho"}]}]}
Upvotes: 2