I am trying to create a test JSON validator in Java (on Fedora 27), but I cannot import JSON-related packages. Do different Java implementations use different packages for this purpose?
My source code is as follows:
import javax.json.JsonObject;
import javax.json.JsonException;
public class JsonParsing {
public static void Main(String[] args) {
String str = "<h1>This is a test.</h1>";
if(isValidJson(str)) {
System.out.println("Valid JSON");
}
else {
System.out.println("JSON Exception detected");
}
}
private static boolean isValidJson(String response) {
try{
JSONObject jsonObj = new JSONObject (response);
} catch(JSONException e) {
System.out.println("JSONException");
return false;
}
return true;
}
}
Error log:
JsonParsing.java:2: error: package javax.json does not exist import javax.json.Json; ^ JsonParsing.java:3: error: package javax.json does not exist import javax.json.JsonObject; ^ JsonParsing.java:18: error: cannot find symbol JSONObject jsonObj = new JSONObject (response); ^ symbol:
class JSONObject location: class JsonParsing JsonParsing.java:18: error: cannot find symbol JSONObject jsonObj = new JSONObject (response); ^ symbol: class JSONObject location: class JsonParsing JsonParsing.java:19: error: cannot find symbol } catch(JSONException e) { ^ symbol: class JSONException location: class JsonParsing 5 errors