简体   繁体   English

如何将json中的响应发送到servlet中的树结构

[英]how to send response in json to a tree structure in servlet

i am new to servlet and i succeeded to send a json format to the client using json-simple package/jar file; 我是servlet的新手,我成功地使用json-simple package / jar文件将json格式发送到客户端; and import it like- 并导入它像 -

import org.json.simple.JSONObject;  

and to get response in json i have following code- 并在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);

and its format is like: 它的格式如下:

{"name":"veshraj joshi","id":"","num":"100","balance":"1000.21"}

and works fine, but i need json format like- 并且工作正常,但我需要json格式 -

{ status:"ok",
  message:"record has been added successfully",
  data:{
        name:"veshraj joshi",
        email:"email@gmail.com",
        address:"kathmandu, Nepal"
     }
 }

and dont have any idea how to achieve this in servlet; 并且不知道如何在servlet中实现这一点;

It works fine while try to make nested json and new code become- response.setContentType("application/json"); 它尝试使嵌套的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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM