将ArrayList转换为json对象

时间:2015-07-16 06:43:36

标签: java json arraylist

我在数据库中有一个表testemployee,其中有三列eid,ename,esalary。我想在json对象中显示表的所有行(后来我想使用kendo UI为json obj提供一个html视图grid)我使用arraylist存储所有数据行然后将列表转换为json。但是当我运行文件时它显示 HTTP状态405 - 此方法不支持HTTP方法GET
以下是我的代码

  package com.JSONoflist;

  import com.google.gson.Gson;

  import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.Statement;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;

@WebServlet(urlPatterns = {"/list2json"})

public class list2json extends HttpServlet {

protected void processRequest(HttpServletRequest request,     HttpServletResponse response) throws IOException {
       response.setContentType("text/html;charset=UTF-8");

    PrintWriter out = response.getWriter();

    Statement stmt = null;

     ResultSet rs = null;
    dbconn Obj = new dbconn();
    Connection connection = Obj.Open();
       try {

      String qry = "SELECT * FROM testemployee";
        stmt = connection.createStatement();
        rs = stmt.executeQuery(qry);

      JSONObject jObj = new JSONObject();
         ArrayList<Employee> list = new ArrayList<Employee>();
         Employee sPojo = null;
         while (rs.next()) {
          sPojo = new Employee();
          sPojo.setId(rs.getString("eid"));
         sPojo.setEname(rs.getString("ename"));
          sPojo.setSalary(rs.getString("esalary"));
          list.add(sPojo);
       }
       //  String json = new Gson().toJson(list);
        System.out.println(list);
        jObj.put("testemployee", list);
       System.out.println(jObj.toString());
    } catch (Exception ex) {
      ex.printStackTrace();
   }
}

}

我也试过Arraylist中的静态值,但http错误。我错过了什么?请帮助我。因为这个错误,我甚至无法检查json是否在url中显示数据库值。

@WebServlet(urlPatterns = {"/list2json"})
public class list2json extends HttpServlet 
{

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

   try {
      List<String> foo = new ArrayList<String>();
      foo.add("A");
       foo.add("B");
      foo.add("C");

      String json = new Gson().toJson(foo);

    } catch (Exception ex) {
      ex.printStackTrace();
   }
}

}

5 个答案:

答案 0 :(得分:1)

只需将您的servlet代码重写为

@WebServlet(urlPatterns = {"/list2json"})
public class list2json extends HttpServlet 
{

   @Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

   try {
      List<String> foo = new ArrayList<String>();
      foo.add("A");
       foo.add("B");
      foo.add("C");

      String json = new Gson().toJson(foo);

    } catch (Exception ex) {
      ex.printStackTrace();
   }
}

答案 1 :(得分:0)

The documentation for HttpServlet清楚地说明了在期望特定HTTP方法时要使用哪些函数。

  • NSDictionary将用于GET请求。
  • doGet将用于POST请求。
  • doPost将用于PUT请求,
  • doPut将用于DELETE请求。

您的方法需要覆盖其中一个,以便它注册一个有效的HTTP方法。

答案 2 :(得分:0)

尝试重命名 : -

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

protected void doGet(HttpServletRequest request, HttpServletResponse response)

我们需要覆盖{/ 1}}获取/发布请求,否则它会调用默认的doGet()/doPost()方法,这些方法具有以下输出功能: -

HTTP状态405 - 此网址不支持HTTP方法GET

详细了解 Servlets here

对于打印列表,请执行以下操作: -

doGet()/doPost()

对于打印/输出,您需要使用protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { List<String> foo = new ArrayList<String>(); foo.add("A"); foo.add("B"); foo.add("C"); String json = foo.toString(); out.print(json); } catch (Exception ex) { ex.printStackTrace(); out.print(ex.getMessage()); } } 对象的out print()

答案 3 :(得分:0)

将此方法添加到servlet中。 您需要从HttpServlet重写方法doGet才能处理GET请求。

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
   processRequest(request, response); 
} 

答案 4 :(得分:0)

使用以下模式:

ArrayList<String> myList = Array.asList(myArray);
myList.add(foo);
myList.add(bar);
JsonArray jsonArray = new JsonArray(myList);