繁体   English   中英

找不到JsonObject类

[英]Cannot find JsonObject class

我有一个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();
    }
}

您的代码应该工作正常,除了此异常' ClassNotFoundException ',这意味着org.json.JSONObject不在类路径和项目构建中,请确保通过maven导入:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20150729</version>
</dependency>

JSONObject类的JAR文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM