14

I used libgdx to build my project and I am having issues using the JSONObject class. When I add import org.json.JSONObject, it says it cannot resolve. How do I add that library to my project?

Here is what I have tried without success:

  1. I downloaded the json-simple-1.1.jar and put it in core/build/libs folder. Could not use import.org.json.simple.JSONObject either.
  2. When browsing the tree in Android Studio as "Packages" dropdown, I can see the classes I want under "Core -> Libraries -> org -> json" but I cannot add them to my project. I get a "cannot refractor, class is in a jar file" error.

Is there something I have not tried yet to solve this issue? I feel I will run into this again as I try to use other external libraries.

Thank you.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Markoh
  • 247
  • 1
  • 2
  • 6
  • 1
    Let' hope you are using Android Studio and the libGdx gradle version(latest). Add the repo in the top most `build.gradle`, http://mvnrepository.com/artifact/org.json/json/20141113. – Jared Burrows Feb 18 '15 at 16:50
  • This worked, thanks! I added: compile 'org.json:json:20141113' to the "core" dependencies in my build.gradle file. – Markoh Feb 19 '15 at 00:03
  • in libgdx there is also in com.badlogic.gdx.utils.Json utility class to avoid import another dependencies – Vokail Dec 29 '17 at 14:56

1 Answers1

31

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'
}
rusheb
  • 1,004
  • 9
  • 15
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • 3
    Please note that this might not be implementation intended as this library may only be used for good - see https://wiki.debian.org/qa.debian.org/jsonevil – koppor Oct 18 '16 at 20:04