If you are using LibGdx, it does not come with a Gradle setup, making multi-project builds streamlined.
You need to add implementation 'org.json:json:<version>' to the dependencies block of your top-level build.gradle.
To find the latest version, see http://mvnrepository.com/artifact/org.json/json/
For example, using the latest version at the time of writing:
dependencies {
implementation 'org.json:json:20200518'
}
Answer from Jared Burrows on Stack OverflowVideos
You are missing json-simple-1.1.1.jar from your classpath.
if you are using Maven, add below into your pom.xml.
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
Or you can download it from here.
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;
Download the ZIP file from this URL and extract it to get the Jar. Add the Jar to your build path. To check the available classes in this Jar use this URL.
To Add this Jar to your build path Right click the Project > Build Path > Configure build path> Select Libraries tab > Click Add External Libraries > Select the Jar file Download
I hope this will solve your problem
You should take the json implementation from here : http://code.google.com/p/json-simple/ .
- Download the *.jar
- Add it to the classpath (right-click on the project, Properties->Libraries and add new JAR.)
In my case i was using maven build tool and got this error so had to add below dependency from here like below and error resolved.
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
Class org.json.JSONObject is available in json-20131018.jar. You need to download this jar and add this jar to your buildpath.
In order to add external jar to buildpath you can - right click on your project in eclipse - click build path -> configure build path - goto tab libraries - there you will find to add external JAR
This will allow you to include any external jar into your build path.
Add json jar to your classpath
or use java -classpath json.jar ClassName
Or add this to your maven pom.xml depedencies:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
As of today (15th July 2020), you need to use latest maven repository as per below:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
For any new version in the future, you can check it here:
- https://mvnrepository.com/artifact/org.json/json
Then simply replace 20200518 with latest new version value.
If you download the JSON jar specified, and list its contents (e.g. with jar tf), it does not contain the org.json.simple package.
So the problem is simply that you need another jar.
EDIT:
I don't know if this is the intent, but an educated guess: if you add this dependency to build.gradle:
compile 'com.googlecode.json-simple:json-simple:1.1.1'
and these imports:
import org.json.simple.parser.*;
// import org.json.simple.*;
import org.json.*;
then the example compiles (for me).
Adding this to my build.gradle file works:
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
Probably your simple json.jar file isn't in your classpath.
I was facing same issue in my Spring Integration project. I added below JSON dependencies in pom.xml file. It works for me.
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
A list of versions can be found here: https://mvnrepository.com/artifact/org.json/json
Just add gradle or maven dependency to your project for gradle:
compile group: 'org.json', name: 'json', version: '20180130'
for maven
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
You need to add the below dependency to your pom.xml.
It will resolve import org.json.simple.JSONObject;
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
Bit late, but I wanted to share my opinion on this.
I faced this problem recently when I found a Java project with both libraries and they were used at the same time.
I think that org.json is easier to read and to use, for 2 main reasons (for my needs):
JsonObject is immutable. You can't add new key/value pairs to an already existing JsonObject (reference here: javax.json: Add new JsonNumber to existing JsonObject)
It takes a few lines to pretty print a JsonObject or JsonArray, while it only takes 1 line to do it with JSONObject or JSONArray. Example:
CopyStringWriter sw = new StringWriter(); Map<String, Object> properties = new HashMap<>(); properties.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory writerFactory = Json.createWriterFactory(properties); JsonWriter jsonWriter = writerFactory.createWriter(sw); jsonWriter.writeObject(jsonObject); //JsonObject created before jsonWriter.close(); String prettyPrintedJSON = sw.toString();
That is the code I use to get an indented JSON to write to a file. And with org.json I only need jsonObject.toString(4).
Another difference is the constructors. You will need a JsonObjectBuilder to create a JSON with javax.json. One step more that can be avoided.
I'm sure there are more differences (not sure if it's possible to create a JsonObject from a String) but these are my thoughts.
JSONObject, as mentioned, is provided by android's API. JsonObject is specifically used for Java EE development which is essentially for web applications and networking capabilities among other things.
The reason Android does not prepackage JsonObject from Oracle Java EE package is because alot of the things javax can do, are not allowed within android like accessing the internet without permission. This means importing the entire jars files of javax would conflict with Android.
If you plan to build your own backend with Java EE, I would highly suggest using JsonObject over JSONObject. On the other hand, if you know a prebuilt rest service or something similar that supports Android's JSON even better.