gpt4 book ai didi

java - 找不到 JsonObject 类

转载 作者:行者123 更新时间:2023-12-01 18:10:13 24 4
gpt4 key购买 nike

我有一个 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>

JAR file对于类(class)JSONObject .

关于java - 找不到 JsonObject 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33617052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com