Java 如何解决“无法解析 org.json.simple.JSONObject”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43813670/
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
How can I solve "org.json.simple.JSONObject cannot be resolved"?
提问by Erik Escriche
When I try to open my jsp through Tomcat, I receive the following messages:
当我尝试通过 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.
无法解析 org.json.simple.JSONObject 类型。它是从所需的 .class 文件间接引用的。
来自 Ejercicio 类型的 getJSONObject() 方法引用了缺少的 JSONObject 类型。
I have a java class that goes like this:
我有一个像这样的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;
}
}
My jsp goes as follows:
我的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());
}
%>
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).
我在 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-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?
当我编译 java 类时,我收到“某些输入文件使用未经检查或不安全的操作”消息。这可能与 jps 没有编译有关吗?
采纳答案by Devendra Lattu
Firstly, you don't need the json-simple.jar
in your project.
Therefore remove the import org.json.simple.*
首先,您json-simple.jar
的项目中不需要。
因此删除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
其次,最佳实践是仅从 jar 中导入所需的类。因此,您可以替换import org.json.*;
为
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;
回答by Sundararaj Govindasamy
You are missingjson-simple-1.1.1.jar
from your classpath.
您的类路径中缺少json-simple-1.1.1.jar
您。
if you are using Maven, add below into your pom.xml.
如果您使用的是Maven,请将以下内容添加到您的 pom.xml 中。
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
Oryou can download it from here.
或者您可以从这里下载。
回答by Suren Aznauryan
Is some version of json-simple.jar
in your project libs ?
json-simple.jar
您的项目库中有某个版本吗?
回答by MD KOUNAIN SHAHI
You can download the requisite jar, for me what worked is the downloading of my missing json20140107 jar and adding the import statements as below:
您可以下载必需的 jar,对我来说,有效的是下载我丢失的 json20140107 jar 并添加如下导入语句:
import org.json.JSONException;
导入 org.json.JSONException;
import org.json.simple.JSONObject;
导入 org.json.simple.JSONObject;
回答by live-love
This works for the org.json package:
这适用于 org.json 包:
Copy the source files from here
从这里复制源文件
https://github.com/stleary/JSON-java
https://github.com/stleary/JSON-java
Note: this is the source code link (JSON-java) taken from from http://www.json.org/
注意:这是来自http://www.json.org/的源代码链接 (JSON-java)
into \src\org\json folder
进入 \src\org\json 文件夹
In Eclipse, click the Open perspective button, then Resource.
Right click on org.json and then hit Refresh to add the java source code into the project.
右键单击 org.json,然后点击 Refresh 将 java 源代码添加到项目中。
回答by Frank HN
for me definetely downloded the jar and add it to eclipse simple
对我来说,我明确地下载了 jar 并将其添加到 eclipse simple
just follow these steps 1. download the .jar file at this link https://jar-download.com/artifacts/org.json2. locate and extract the json.jar file
只需按照以下步骤操作 1. 在此链接https://jar-download.com/artifacts/org.json下载 .jar 文件 2. 找到并解压 json.jar 文件
in eclipse -right click on your project and go to build path -add external jar -hit apply
在 Eclipse 中 - 右键单击您的项目并转到构建路径 - 添加外部 jar -hit apply
回答by terribleProgrammer
I didn't see the right answer anywhere but my TomCat JSON library problem was with the artifact deployment. If you're using intelliJ, go to project structure >> artifacts and add the jar to your lib folder
我在任何地方都没有看到正确的答案,但我的 TomCat JSON 库问题与工件部署有关。如果您使用的是 intelliJ,请转到项目结构 >> 工件并将 jar 添加到您的 lib 文件夹中
Check out my project structure
查看我的项目结构