后台servlet从数据库获得list集合,怎么把list转换成json数据传到jsp页面?

2024-12-13 05:30:43
推荐回答(1个)
回答1:

public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
JSONObject json = new JSONObject();
json.put("data1", "1233");
json.put("data2", "2344");
ServletOutputStream os = resp.getOutputStream();
os.write(json.toString().getBytes());
os.flush();
os.close();
}
list转JSON时需要用循环进行处理,并将对应的KEY写入到JSON中