Reputation: 103
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
Upvotes: 0
Views: 5136
Reputation: 18568
String jsonTxt = "{'lhs': '100 Euros','rhs':'128.551738 Australian dollars','error':'','icc': 'true'}";
JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );
System.out.println( "lhs: " + json.getString("lhs") );
System.out.println( "rhs: " + json.getString("rhs") );
System.out.println( "error: " + json.getString("error") );
System.out.println( "icc: " + json.getString("icc") );
OUTPUT:
lhs: 100 Euros
rhs: 128.551738 Australian dollars
error:
icc: true
you can give the json string with double quotes(") or single quotes(') or key without quotes. All works.
you need following jars:
1. commons-lang-2.4.jar
2. ezmorph-1.0.jar
3. json-lib-0.9.jar
for adding the jars through eclipse:
1.right click on project folder
2.click on prperties
3.select "java build path"
4.select libraries tab
5.click on "Add External jars"
6.Browse your jars, select and click ok.
Upvotes: 1
Reputation: 80176
You doesn't have Apache commons-lang in your runtime classpath.
Upvotes: 0
Reputation: 20061
You're missing the Apache Commons lang library from your classpath.
If you ever are stumped by a NoClassDefFoundError
try plugging the class name into jarFinder - that will tell you jar files where the class can be found.
Upvotes: 2
Reputation: 371
You have a hanging "," after 'true'.
And technically strings and property names in JSON should use double quotes, not single quotes.
Upvotes: 0