[英]Converting ArrayList into JSON Array, Java Programming
Hi there i know maybe this is stupid mistake, but i need your help. 您好,我知道这可能是愚蠢的错误,但我需要您的帮助。
i have code like this that getting an object from db using ArrayList then convert it to JSONArray 我有这样的代码,使用ArrayList从数据库获取对象,然后将其转换为JSONArray
protected JSONArray statisticPesanan(){
List<ListPesanan> det = new ArrayList();
JSONArray jsonArray = new JSONArray();
try {
//create JSONObject to executed
String sql = "SELECT * FROM vstatisticpesanan";
Connection conn;
conn = datman.logOn();
Statement statement = (Statement) conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
//add list to Array
while (rs.next())
{
ListPesanan psn = new ListPesanan(rs.getString(1), rs.getInt(2));
det.add(psn);
}
//convert to JSON array
for (int i=0; i < det.size(); i++) {
jsonArray.add(det.get(i).jsonobject());
}
} catch (SQLException se) {
Logger.getLogger(Analytics.class.getName()).log(Level.SEVERE, null, se);
java.lang.System.out.println("Error on Analytics Generate JSON Pesanan Statistics : " + se.toString());
}
finally {
datman.logOff();
}
return jsonArray;
}
the output from servlet execution using this code 使用此代码执行servlet的输出
public class chartGetTransaction extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* Execution of JSON Chart */
Analytics show = new Analytics();
JSONArray array = show.getStatisticPesanan();
for(int i = 0; i < array.size(); i++){
out.print(array.get(i).toString());
}
} finally {
out.close();
}
}
the RESULT OUTPUT is like this: 结果输出是这样的:
{"1":"2013-10-12","2":3}{"1":"2013-11-16","2":2}{"1":"2013-12-23","2":4}{"1":"2014-02-11","2":10}
but i need the format like this:: 但我需要这样的格式:
[{"1":"2013-10-12","2":3},{"1":"2013-11-16","2":2},{"1":"2013-12-23","2":4},{"1":"2014-02-11","2":10}]
any ideas guys? 有什么想法吗?
thanks. 谢谢。
A JSONArray
contains other json elements. JSONArray
包含其他json元素。 In your case, it seems like it contains json objects. 就您而言,它似乎包含json对象。
When you do 当你做
JSONArray array = show.getStatisticPesanan();
for(int i = 0; i < array.size(); i++){
out.print(array.get(i).toString());
}
you are printing (and concatenating) the individuals objects in the array. 您正在打印(并连接)阵列中的各个对象。 You don't want this. 你不要这个 You want to print the array directly. 您要直接打印阵列。 Fortunately, JSONArray#toString()
already does that serialization for you. 幸运的是, JSONArray#toString()
已经为您完成了序列化。
JSONArray array = show.getStatisticPesanan();
out.print(array.toString());
Because you are using the for loop 因为您正在使用for循环
for(int i = 0; i < array.size(); i++){
out.print(array.get(i).toString());
}
it will print the one - by - one for the requirement format you just have to use the below code.. 它将使用您需要的以下代码逐一打印需求格式。
StringWriter out = new StringWriter();
jsonArray.writeJSONString(out);
String jsonText = out.toString();
out.print(jsonText);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.