找不到JsonObject类

时间:2015-11-09 19:49:04

标签: java servlets

我有一个servlet,它从请求中获取一个参数并发送JsonObject作为响应。

我的问题是每次我尝试使用PrintWriter类(我甚至尝试打印简单字符串作为测试)时,servlet启动但从不返回响应。我的第二个问题是我得到以下异常:

java.lang.ClassNotFoundException: org.json.JSONObject

什么包包含JsonObject?

servlet代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String user = request.getParameter("param1"); // I need this later ...
    System.out.println("do get called ");
    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");
    PrintWriter out = response.getWriter();
    org.json.JSONObject json = new org.json.JSONObject();
    try {
        json.put("Mobile",123);
        json.put("Name", "ManojSarnaik");
        out.print(json.toString());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        out.flush();
    } finally{
        out.flush();
    }
}

1 个答案:

答案 0 :(得分:4)

除了此异常“ClassNotFoundException”之外,您的代码应该正常工作,这意味着org.json.JSONObject不在类路径和项目构建中,请确保按{maven导入1}}:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20150729</version>
</dependency>
课程JAR file

JSONObject