How can I solve "org.json.simple.JSONObject cannot be resolved"?

Erik Escriche :

When I try to open my jsp through Tomcat, I receive the following messages:

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.

I have a java class that goes like this:

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

My jsp goes as follows:

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

I have added a lot of jars to both the lib folder inside WEB-INF and the Tomcat lib trying to solve the problem with no results (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-sources, org.json).

When I compiled the java class I got the "Some input files use unchecked or unsafe operations" message. Could this be related to the jps not compiling?

Devendra Lattu :

Firstly, you don't need the json-simple.jar in your project.
Therefore remove the import org.json.simple.*

Secondly, the best practice is to import only the required classes from your jar.Therefore you can replace import org.json.*; by

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=471090&siteId=1