【发布时间】:2015-03-11 18:42:38
【问题描述】:
我在 jsp 中编写了一个程序来将值插入数据库,现在我想从数据库中检索这些值并以 json 格式显示。 我有姓名、性别和年龄参数,我在一个列表中得到它,我想将它显示为 用户{ 1:{姓名:abc,性别:男,年龄:21},2:{姓名:xyz,性别:女,年龄:25},...... }
我的 Jsp 代码
<%
PrintWriter outt = response.getWriter();
JSONObject obj = new JSONObject();
Insert_UserDetails details = new Insert_UserDetails();
request.setCharacterEncoding("utf8");
response.setContentType("application/json");
List<String> list = details.getAllUsers();
JSONArray jArray = new JSONArray();
for (int i = 0; i < list.size(); i++) {
JSONObject formDetailsJson = new JSONObject();
formDetailsJson.put("result", list.get(i));
jArray.add(formDetailsJson);
}
obj.put("form_details", jArray);
out.print(obj.toString());
%>
我得到的输出是:
{"form_details":[{"result":"arjun"},{"result":"male"},{"result":"21"},
{"result":"ravi"},{"result":"male"},{"result":"30"},{"result":"pushpa"},
{"result":"female"},{"result":"57"},{"result":"usha"},{"result":"female"},
{"result":"60"},{"result":"bharat"},{"result":"male"},{"result":"30"},
{"result":"ramesh"},{"result":"male"},{"result":"29"},{"result":"ramesh"},
{"result":"male"},{"result":"29"}]}
我是 json 新手,所以需要一些指导 谢谢你
【问题讨论】: