0

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();
        } ```

1 Answers1

0

Your problem has nothing to do with JSON parsing, it's about how to use a Java library.

The code you posted shows that you're importing classes like org.json.simple.JSONObject which are not part of the Java standard library, hence you need to provide them to the Java runtime, usually by including the jar, which is basically a zip file containing class files (.class files, the compiled version of a .java file), into the so-called classpath, which is where the Java runtime looks for classes to load.

Here's an example of how you could run your program in case you already had the simple-json jar at libs/simple-json.jar, and you already compiled your Solution class:

java -cp libs/simple-json.jar Solution

With this, the Java runtime will find the JSON classes inside the jar and everything should work.

If you need more jars in the classpath, you can separate them using : on Mac/Linux or ; on Windows.

For example:

java -cp libs/simple-json.jar:libs/other-lib.jar Solution

For detailed information about how this works, see this answer.

Renato
  • 12,940
  • 3
  • 54
  • 85