如何将 json 中的响应发送到 servlet 中的树结构
how to send response in json to a tree structure in servlet
我是 servlet 的新手,我成功地使用 json-简单 package/jar 文件向客户端发送了 json 格式;并将其导入 -
import org.json.simple.JSONObject;
为了在 json 中得到响应,我有以下代码-
response.setContentType("application/json");
JSONObject obj = new JSONObject();
obj.put("name", "veshraj joshi");
obj.put("id",request.getParameter("id"));
obj.put("num", new Integer(100));
obj.put("balance", new Double(1000.21));
out.println(obj);
其格式如下:
{"name":"veshraj joshi","id":"","num":"100","balance":"1000.21"}
并且工作正常,
但我需要 json 这样的格式-
{ status:"ok",
message:"record has been added successfully",
data:{
name:"veshraj joshi",
email:"email@gmail.com",
address:"kathmandu, Nepal"
}
}
并且不知道如何在 servlet 中实现这一点;
尝试嵌套 json 时工作正常,新代码变为 -
response.setContentType("application/json");
JSONObject obj = new JSONObject();
JSONObject obj1 = new JSONObject();
obj1.put("email",'email@gmail.com');
obj1.put("name", "veshraj joshi");
obj1.put("id",request.getParameter("id"));
obj1.put("num", new Integer(100));
obj1.put("balance", new Double(1000.21));
obj.put("status","ok");
obj.put("message","record has been added successfully");
obj.put("data",obj1);
out.println(obj);
我是 servlet 的新手,我成功地使用 json-简单 package/jar 文件向客户端发送了 json 格式;并将其导入 -
import org.json.simple.JSONObject;
为了在 json 中得到响应,我有以下代码-
response.setContentType("application/json");
JSONObject obj = new JSONObject();
obj.put("name", "veshraj joshi");
obj.put("id",request.getParameter("id"));
obj.put("num", new Integer(100));
obj.put("balance", new Double(1000.21));
out.println(obj);
其格式如下:
{"name":"veshraj joshi","id":"","num":"100","balance":"1000.21"}
并且工作正常, 但我需要 json 这样的格式-
{ status:"ok",
message:"record has been added successfully",
data:{
name:"veshraj joshi",
email:"email@gmail.com",
address:"kathmandu, Nepal"
}
}
并且不知道如何在 servlet 中实现这一点;
尝试嵌套 json 时工作正常,新代码变为 - response.setContentType("application/json");
JSONObject obj = new JSONObject();
JSONObject obj1 = new JSONObject();
obj1.put("email",'email@gmail.com');
obj1.put("name", "veshraj joshi");
obj1.put("id",request.getParameter("id"));
obj1.put("num", new Integer(100));
obj1.put("balance", new Double(1000.21));
obj.put("status","ok");
obj.put("message","record has been added successfully");
obj.put("data",obj1);
out.println(obj);