“在jsp上编写Java代码是一种不好的做法”。虽然可以写。如我所见,列表包含字符串格式的所有详细信息,每个第3个索引间隔都有一个新的记录详细信息,您可以这样编写。
<% PrintWriter outt = response.getWriter();JSonObject obj = new JSonObject();JSonObject finalJSON = new JSonObject();Insert_UserDetails details = new Insert_UserDetails();request.setCharacterEncoding("utf8");response.setContentType("application/json");List<String> list = details.getAllUsers();int recordCounter=1;JSonArray jArray = new JSonArray();for (int i = 0; i < list.size(); i++) { JSonObject formDetailsJson = new JSonObject(); formDetailsJson.put("name", list.get(i)); formDetailsJson.put("gender", list.get(++i)); formDetailsJson.put("age", list.get(++i)); finalJSON.put(recordCounter,formDetailsJson); recordCounter++;}out.print(finalJSON.toString());%>上面的代码将像您在问题中提到的那样输出。确保您的列表包含3条记录的倍数,否则可能会出现
IndexOutOfBound异常。



