
Untitled
By: a guest on
May 4th, 2012 | syntax:
None | size: 1.15 KB | hits: 16 | expires: Never
Basic JSON-lib example
import net.sf.json.*;
public class hello {
public static void main(String[] args) {
String settings = "{"hello": "world"}";
JSONObject obj = (JSONObject)JSONSerializer.toJSON(settings);
System.out.println(obj.toString());
}
}
javac -cp lib/json-lib-2.4-jdk15.jar hello.java
java -cp .:lib/json-lib-2.4-jdk15.jar:lib/commons-lang-2.4.jar hello
commons-beanutils-1.8.0
commons-collections-3.2.1
commons-lang.2.5
commons-logging-1.1.1
ezmorph-1.0.6
xom.1.1 (if serializing from/to XML)
oro-2.0.8 (if using the jdk13 version of the library)
import com.google.gson.Gson;
class Foo {
private String hello;
public String toString() {
return "hello='" + hello + "'";
}
}
public class hello {
public static void main(String[] args) {
String text = "{"hello": "world"}";
Gson gson = new Gson();
Foo foo = gson.fromJson(text, Foo.class);
System.out.println(foo.toString());
System.out.println(gson.toJson(foo));
}
}
$ javac -cp lib/gson-2.0.jar hello.java
$ java -cp .:lib/gson-2.0.jar hello
hello='world'
{"hello":"world"}
$