如何在JSP中将字符串列表添加到JSON对象中? [英] how to add a list of string into json object in jsp?

查看:292
本文介绍了如何在JSP中将字符串列表添加到JSON对象中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用jsp编写了一个将值插入数据库的程序,现在我想从数据库中检索这些值并以json格式显示. 我有姓名,性别和年龄参数,我将其显示在列表中,我想将其显示为 用户{ 1:{name:abc,gender:male,age:21},2:{name:xyz,gender:female,age:25},...... }

I have written a program in jsp to insert values into database , now i want to retrieve those values from database and show it in json format. I have name , gender and age parameters and i am getting it in a list and i want to show it as Users{ 1:{name:abc,gender:male,age:21},2:{name:xyz,gender:female,age: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的新手,所以需要一些指导 谢谢

I am new to json , so need some guidance Thank You

推荐答案

在jsp上编写Java代码是一种不好的做法".虽然可以写.如我所见,列表包含字符串格式的所有详细信息,每个第3个索引间隔都有一个新的记录详细信息,您可以这样编写.

'Writing java code on jsp is a bad practice'. Though you can write. As I can see you list contains all details in string format and every 3rd index interval there is a new record details to do so you can write like.

   <%
 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异常.

Above code will output like you mentioned in your question. Make sure that your list contains records multiple of 3 otherwise you may get IndexOutOfBound exception.

这篇关于如何在JSP中将字符串列表添加到JSON对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆