Imported json library Java, but still getting JSON cannot be resolved error

400 Views Asked by At

I started coding on leetcode and then opened it in playground and put that code in my local and imported the json jar, but am still seeing the error that Json cannot be resolved.

error line: return Json.value(input).toString();

I have tried the both import jars

import org.json.; import org.json.simple.;

Any help would be greatly appreciated.

Thanks in advance.

public static String stringToString(String input) {
    if (input == null) {
    return "null";
        }
        return Json.value(input).toString();
}
2

There are 2 best solutions below

1
priteshbaviskar On

If you are trying to run this on leetcode, the jar should be actually present on your application classpath in order for import to work. As a test, try running your code in some IDE like eclipse or Intellij. It will prompt you at compile time about the error.

0
Veng On

Json not available in org.json. You may use JSONObject to parse json string like this

JSONObject jo = new JSONObject(
  "{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}"
);

See full example Here