i write this code to read json data , and there is an error when run the code
first this is the code i write ( i changed the code to correct the json string but the problem still exist )
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class defaults {
public static void main(String[] args) {
String jsonTxt = "{lhs: \"100 Euros\",rhs: \"128.551738 Australian dollars\",error: \"\",icc: true}";
JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );
String title = json.getString("title");
System.out.println( "title: " + title );
}
}
and i have found this error when run the code
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source).....
the error gone away if i remove lines that talks about json
OUTPUT:
you can give the json string with double quotes(“) or single quotes(‘) or key without quotes. All works.
you need following jars:
for adding the jars through eclipse: