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>
Answer from Alien on Stack OverflowIn 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.
Your dependency and import looks correct. Reloading all maven dependencies should resolve the issue. Check this post out: Force Intellij IDEA to reread all maven dependencies
I had the same problem, added org.json dependency and reloaded maven, But still Intellij didn't recognize it.
I tried write following code in module-info.java , then Intellij recognized it:
requires org.json;
error: package org.json does not exist
Currently, org.json.jar sits in the same path
Java expects packages to be in a folder structure.
More on reddit.comjava - Cannot resolve json import Intellij after adding dependency and library .jar - Stack Overflow
package does not exist
Can't import "json-20240303" library into my project.
Videos
I'm parsing json files using JSONArray and JSONObject but I keep getting the error shown in the title. I've downloaded org.json.jar but I don't know what to do next. Currently, org.json.jar sits in the same path as all of the other java files and classes.
Thanks!
I'm going to start by saying I'm a complete novice in Java. I've started computer science recently and I've got a project where I have to create a project to represent a server using sockets. The server will has to function as a control a library's book record/register. My professor provided a .json file with the book lists which I have to use.
I tried creating the project on VS Code, but after following these steps:
Downloaded the library from the Maven Repository.
Created a lib folder in your project's root directory and move the json-20230303.jar file to this folder.
My code couldn't import the library and this error appeared every time I tried to use the library:
"src\Gerenciador.java:2: error: package org.json does not exist import org.json.JSONArray;"
After a while I thought it would be easier to import the library on the Ide Eclipse, but after creating the project and adding it via the Build Path option, it still didn't work. Even though the library appeared in the Libraries section in the Java Build Path.
This error: "The type org.json.JSONObject is not accessible" keeps appearing.
I don't know how to fix the problem. I think I'm calling on the library wrong, but I don't know how I should do it. If there is any information missing, let me know and I'll provide it, it's my first time asking for help on a programming forum.
I have been trying to figure out this issue forever now but I can't seem to see what I'm doing wrong. I am using VSCode and added the json library (json-20220329.jar) to my referenced libraries. When I add the jar file the errors in my main file go away regarding JSONObject and JSONArray. Everything looks fine until I try running the code and it says that the "package org.json does not exist." I was wondering if anyone knew what could be the problem.
CODE: (App.java)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;
import org.json.JSONArray;
public class App {
public static void main(String[] args) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://jsonplaceholder.typicode.com/albums")).build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
public static String parse(String responseBody) {
JSONArray albums = new JSONArray(responseBody);
for (int i = 0; i < albums.length(); i++)
{
JSONObject album = albums.getJSONObject(i);
int id = album.getInt("id");
int userID = album.getInt("userId");
String title = album.getString("title");
System.out.println(id + "\t" + title + "\t" + userID);
}
return null;
}
}ERROR:
App.java:8: error: package org.json does not exist
import org.json.JSONObject;
^
App.java:9: error: package org.json does not exist
import org.json.JSONArray;
^
App.java:26: error: cannot find symbol
JSONArray albums = new JSONArray(responseBody);
^
symbol: class JSONArray
location: class App
App.java:26: error: cannot find symbol
JSONArray albums = new JSONArray(responseBody);
^
symbol: class JSONArray
location: class App
App.java:29: error: cannot find symbol
JSONObject album = albums.getJSONObject(i);
^
symbol: class JSONObject
location: class App
5 errorsI don't know if this may help but I've tried all of the solutions mentioned here and more. NOTHING SEEMS TO WORK.
I even look every related issues raised on github issues in Jetbrains and Nothing.
And I was about to loose my mind and started pulling out my hairs and something cames out to my mind.
What I did was use a <dependencyManagement> </dependencyManagement> on my project and specify all the versions and everything then mvn clean install and then on another project I added the dependency as we normally do then IntelliJ, Vscode, started to recogize my imports and classes.
I hope it will also work for you.
If you do not want to destroy .idea, you can try :
- open Project Structure > Modules
- unmark the java folder as a source folder
- apply / rebuild
- then mark it again as a source folder
- rebuild
cloud1's answer helped me out:
Try pressing 'Reimport All Maven Projects' button. It's the first button at Maven Projects tab (blue circle). Looks like this dependency wasn't downloaded from Internet yet.
I have made some test, and on my computer with fresh repositorys it is working, on the old one not.
In intellij go to settings (ctrl alt s) -> Build,execution,deployment -> Build Tools -> Maven -> Repositorys
Click on maven -> and update on the right corner.
Trying to use JSON to store some objects locally for retrieval later. Can't figure out why I'm unable to import json. Doesn't work in vim, doesn't work in vsCode, doesn't work in Eclipse.
Read online that I may need to download the json .jar and add to my build path? When googling "json download" its only scammy virus websites.
I'm on a Mac.
Edit: Added pictures
https://imgur.com/a/FlKwMIF
Thanks for the help.
if you create the project with Maven you are going to avoid all the import and compile problems and you will focus only on code. If you are new to Maven, it´s simpler than you can think. To create the proyect see this short video
Working with Maven in IntelliJ IDEA
Then you can easily import JSON.simple library adding the following dependency to pom.xml file, inside tags:
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
In real projects you are going to use Maven or Gradle, so the soon you learn that is better. If continue with errors even with Maven or you need to solve this without Maven, post a reproducer or even your project.
Problem solved.
It turns out that I should use the javac -cp .;json-simple-1.1.1.jar C:\Users\yiges\Desktop\COMP90015\Project3\WhiteBoard\src\Main.java command to compile the code