Java 从servlet创建自定义json
我是json新手。我能够从servlet创建json。但我必须创建一个json,如下所示-Java 从servlet创建自定义json,java,json,servlets,Java,Json,Servlets,我是json新手。我能够从servlet创建json。但我必须创建一个json,如下所示- { "name":"Employee", "children":[{ "name":"Subho" }, { "name":"jeet", "children":[{ "name":"rahul" }, { "name":"abhijit" }] }] } 但我创造的是- { "children":[ {"name
{
"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();
}
}
公共类ActionServlet扩展了HttpServlet{
受保护的void processRequest(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
setContentType(“text/html;charset=UTF-8”);
PrintWriter out=response.getWriter();
试一试{
随机r=新随机();
int低=0;
int高=5;
int R=R.nextInt(高-低)+低;
/*子节点的示例数据此处将调用实际数据*/
字符串arr[]={“文化”、“工资”、“工作”、“经济”};
/*负责创建子节点及其名称*/
Map mapping=newhashmap();
EntryListContainer EntryListContainer=新的EntryListContainer();
List entryList1=new ArrayList();
对于(int i=0;i
这是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;
}
公共类EntryListContainer{
私人名单儿童;
私有字符串名称;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共无效setEntryList1(列表entryList1){
this.children=entryList1;
}
公共列表getEntryList1(){
返回儿童;
}
这是DynamicFieldName策略类
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();
}
公共类dynamicFieldName策略实现FieldNaming策略{
私人地图测绘;
公共动态字段命名策略(地图映射){
this.mapping=映射;
}
@凌驾
公共字符串translateName(字段){
字符串newName=mapping.get(field.getName());
if(newName!=null){
返回newName;
}
返回字段。getName();
}
这个servlet代码正在创建一个json。首先,我创建了所有子节点并将它们放在一个列表中(这里是entryList1),然后将它们放在一个hashmap中。但我创建的并没有满足我的要求
请任何人帮我解决这个问题。如果我们将您的JSon放到,我们将得到: 现在我们可以看到每个节点都有名称和其他节点的列表: 节点
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);
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"}]}]}