java 找不到 JsonObject 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33617052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:55:44  来源:igfitidea点击:

Cannot find JsonObject class

javaservlets

提问by ghassen92

I have a servlet that takes a parameter from the request and sends a JsonObjectas a response.

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

My problem that every time that I tried to use the PrintWriterclass (I tried even to print simple string as a test), the servlet starts but never returns a response. My second problem is that I get the following exception:

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

java.lang.ClassNotFoundException: org.json.JSONObject

What package contains the JsonObject?

哪个包包含 JsonObject?

The servlet code:

服务程序代码:

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();
    }
}

回答by Husam

Your code should be worked fine except for this exception 'ClassNotFoundException' that means that the org.json.JSONObjectaren't in the classpath and on the build for project, make sure that you import by maven:

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

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

or JAR filefor the class JSONObject.

JSONObject类的JAR 文件