java simple json从数据库获取数据并显示
我想问一下如何生成desire json输出,如下所示:java simple json从数据库获取数据并显示,java,json,Java,Json,我想问一下如何生成desire json输出,如下所示: { "Result":"OK", "Records":[ {"PersonId":1,"Name":"Benjamin Button","Age":17,"RecordDate":"\/Date(1320259705710)\/"}, {"PersonId":2,"Name":"Douglas Adams","Age":42,"RecordDate":"\/Date(1320259705710)\/"}, {"Person
{
"Result":"OK",
"Records":[
{"PersonId":1,"Name":"Benjamin Button","Age":17,"RecordDate":"\/Date(1320259705710)\/"},
{"PersonId":2,"Name":"Douglas Adams","Age":42,"RecordDate":"\/Date(1320259705710)\/"},
{"PersonId":3,"Name":"Isaac Asimov","Age":26,"RecordDate":"\/Date(1320259705710)\/"},
{"PersonId":4,"Name":"Thomas More","Age":65,"RecordDate":"\/Date(1320259705710)\/"}
]
}
<%@page language="java" import="java.sql.*"%>
<%@page import="java.util.*" %>
<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONArray"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="org.json.simple.parser.JSONParser"%>
<%@page import="org.json.simple.parser.ParseException"%>
<%
String dept = (String)request.getParameter("dept");
String sql = "SELECT * FROM employees WHERE department='"+dept+"'";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=null;
conn=DriverManager.getConnection("jdbc:mysql://localhost/jspjsons","root","123456");
ResultSet rs=null;
Statement stm1=conn.createStatement();
JSONArray list = new JSONArray();
rs=stm1.executeQuery(sql);
while(rs.next())
{
JSONObject obj=new JSONObject();
obj.put("PersonId", rs.getString("id"));
obj.put("Name", rs.getString("name"));
obj.put("Age", rs.getString("age"));
obj.put("RecordDate", rs.getString("date"));
list.add(obj);
}
out.print(list);
}
catch(Exception ex)
{
out.println("<h1>"+ex+"</g1>");
}
%>
我的jsp代码如下所示:
{
"Result":"OK",
"Records":[
{"PersonId":1,"Name":"Benjamin Button","Age":17,"RecordDate":"\/Date(1320259705710)\/"},
{"PersonId":2,"Name":"Douglas Adams","Age":42,"RecordDate":"\/Date(1320259705710)\/"},
{"PersonId":3,"Name":"Isaac Asimov","Age":26,"RecordDate":"\/Date(1320259705710)\/"},
{"PersonId":4,"Name":"Thomas More","Age":65,"RecordDate":"\/Date(1320259705710)\/"}
]
}
<%@page language="java" import="java.sql.*"%>
<%@page import="java.util.*" %>
<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONArray"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="org.json.simple.parser.JSONParser"%>
<%@page import="org.json.simple.parser.ParseException"%>
<%
String dept = (String)request.getParameter("dept");
String sql = "SELECT * FROM employees WHERE department='"+dept+"'";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=null;
conn=DriverManager.getConnection("jdbc:mysql://localhost/jspjsons","root","123456");
ResultSet rs=null;
Statement stm1=conn.createStatement();
JSONArray list = new JSONArray();
rs=stm1.executeQuery(sql);
while(rs.next())
{
JSONObject obj=new JSONObject();
obj.put("PersonId", rs.getString("id"));
obj.put("Name", rs.getString("name"));
obj.put("Age", rs.getString("age"));
obj.put("RecordDate", rs.getString("date"));
list.add(obj);
}
out.print(list);
}
catch(Exception ex)
{
out.println("<h1>"+ex+"</g1>");
}
%>
另外,在显示输出时,始终会出现像这样的前后支架[],如何消除它?需要它以{}而不是[]开始和结束如果您正在正确地创建
记录
数组(存储在列表
变量中),则只需将其与结果一起添加到新的JSONObject
请记住,{…}
表示JSONObject
,[…]
表示JSONArray
while(rs.next())
{
JSONObject obj=new JSONObject();
obj.put("PersonId", rs.getString("id"));
obj.put("Name", rs.getString("name"));
obj.put("Age", rs.getString("age"));
obj.put("RecordDate", rs.getString("date"));
list.add(obj);
}
//Include this code beneath to create the JSON you require (mainObject).
JSONObject mainObject = new JSONObject();
mainObject.put("Result", "OK");
mainObject.put("Records", list);
如果正确地创建了记录
数组(存储在列表
变量中),则只需将其与结果一起添加到新的JSONObject
请记住,{…}
表示JSONObject
,[…]
表示JSONArray
while(rs.next())
{
JSONObject obj=new JSONObject();
obj.put("PersonId", rs.getString("id"));
obj.put("Name", rs.getString("name"));
obj.put("Age", rs.getString("age"));
obj.put("RecordDate", rs.getString("date"));
list.add(obj);
}
//Include this code beneath to create the JSON you require (mainObject).
JSONObject mainObject = new JSONObject();
mainObject.put("Result", "OK");
mainObject.put("Records", list);
您的代码意味着您需要对象数组,因为它必须以[]开头。如何使它以{}开头和结尾?与上面所需的输出一样,您不需要JSONArray list=new JSONArray();相反,您需要在其中直接使用JSONObject,您可以定义JSONArray属性并填充它。您的代码意味着您需要对象数组,因为它必须以[]开头。如何让它以{}开头和结尾?与上面所需的输出一样,您不需要JSONArray list=new JSONArray();相反,您需要在其中直接使用JSONObject,您可以定义JSONArray属性并填充它;正确吗?是的,mainObject
应该看起来像您在问题顶部包含的所需JSON。您的意思是使用JSONObject而不是像out.print(mainObject)这样的JSONArray;正确吗?是的,mainObject
应该与问题顶部包含的所需JSON相似。