I'm trying to convert scanner string into JSONObject in Java and import JSON modules from Json Simple. I have already added JSON-simple-1.1.1.jar into my build path. However, when I was trying to paser my inline string. My inline string looks like this: {"events":[{"url":"/manipulate-cloudy-country","visitorId":"03c0b5e0-3eeb-382a-8c6d-35e4bb189cf6","timestamp":1515042087205},...{ "url":"/race-lazy-control", "visitorId":"0832d11b-017a-388a-8a9e-77e5a1f6f9f4", "timestamp":1515042342655 }]} It should be a bunch of data that I try to convert into JSONObject.
The complier shows: Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject at Solution.main(Solution.java:45) Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ... 1 more
How can I fix it, or is there any better solution to convert it? Please help me!
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class Solution {
public static void main(String[] args) {
try {
//URL url = new URL(
"My API");
/**
*{"events":[{"url":"/manipulate-cloudy-country","visitorId":"03c0b5e0-3eeb-382a-8c6d-35e4bb189cf6","timestamp":1515042087205},...{"url":"/shoe-boundless-curtain","visitorId":"e367b3c8-3d48-345d-937c-0c4f6cce216a","timestamp":1515042137934},{"url":"/race-lazy-control","visitorId":"0832d11b-017a-388a-8a9e-77e5a1f6f9f4","timestamp":1515042342655}]}
**/
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
throw new RuntimeException("HttpResponseCode: " + responseCode);
} else {
String inline = "";
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
inline += scanner.nextLine();
}
// Close the scanner
scanner.close();
JSONObject obj = new JSONObject();
JSONParser p = new JSONParser();
p.parse(inline);
}
} catch (Exception e) {
e.printStackTrace();
} ```