java - 我该如何解决 "org.json.simple.JSONObject cannot be resolved"?

标签 java json jsp tomcat jar

当我尝试通过 Tomcat 打开我的 jsp 时,我收到以下消息:

The type org.json.simple.JSONObject cannot be resolved. It is indirectly referenced from required .class files.

The method getJSONObject() from the type Ejercicio refers to the missing type JSONObject.

我有一个像这样的 java 类:

    package E;
    import org.json.simple.*;
    import javax.json.*;
    import org.json.*;

    public class Ejercicio {
        public int a;
        public String b;

    public Ejercicio (int a, String b) {
        this.a=a;
        this.b=b;
        }

    public JSONObject getJSONObject() {
        JSONObject obj = new JSONObject();
        obj.put("a", a);
        obj.put("b", b);
        return obj;
        }
    }

我的jsp如下:

    <%@page import="java.io.*" %>
    <%@page import="java.util.*"%>
    <%@page import="E.Ejercicio"%>
    <%@page import="javax.json.*"%>
    <%@page import="org.json.simple.*"%>
    <%@page import="org.json.*"%>
    <%@page import="net.sf.json.*" %>

    <%  
    ArrayList<Ejercicio> miArray = new ArrayList<Ejercicio>();
    miArray.add(new Ejercicio (1,"Hola"));
    miArray.add(new Ejercicio (2,"Caracola"));
    miArray.add(new Ejercicio (3,"Perola"));
    for (Ejercicio temp:miArray) {
        out.println("<p>"+temp.b+"</p>");
        }

    JSONArray jsonArray = new JSONArray();
    for (int i=0; i < miArray.size(); i++) {
        jsonArray.put(miArray.get(i).getJSONObject());
        }
    %>

我已经在 WEB-INF 中的 lib 文件夹和 Tomcat lib 中添加了很多 jar 试图解决问题但没有结果(commons-beanutils-1.8.1,commons-collections-3.2.1,commons- lang-2.5, commons-logging-1.1.1, ezmorph-1.0.6, java-json, javax.json-1.0, json-lib-2.4-jdk15, json-rpc-1.0, json-simple-1.1.1-来源,org.json)。

当我编译 java 类时,我收到“一些输入文件使用未经检查或不安全的操作”消息。会不会和jps没有编译有关?

最佳答案

您的类路径中缺少 json-simple-1.1.1.jar

如果您使用的是 Maven,请将以下内容添加到您的 pom.xml 中。

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

或者您可以从here 下载它.

关于java - 我该如何解决 "org.json.simple.JSONObject cannot be resolved"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43813670/

相关文章:

jsp - 未指定默认命名空间时,函数 getMessageData 必须与前缀一起使用

java - 即使提供缓存参数后,JSP 页面也会被缓存

java - 在 Spring 请求中验证枚举

java - SSH 输出始终为空

arrays - 如何将 Json 转换为二维数组?

javascript - Google Maps API 路线服务以 xml 格式返回路线

java - 如何在 Android Retrofit api 中为每个 api 使用 OkHttp 拦截器获取 api 请求和 api 响应时间?

java - 什么是 @JsonTypeInfo 和 @JsonSubTypes 在 jackson 中的用途

javascript - 无法使用 Angular JS 获取纯文本格式的 JSON 数据

java - 从结果集中选择(单击而不是 SQL Select)特定结果