Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.15 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Basic JSON-lib example
  2. import net.sf.json.*;
  3.  
  4. public class hello {
  5.     public static void main(String[] args) {
  6.         String settings = "{"hello": "world"}";
  7.  
  8.         JSONObject obj = (JSONObject)JSONSerializer.toJSON(settings);
  9.         System.out.println(obj.toString());
  10.     }
  11. }
  12.        
  13. javac -cp lib/json-lib-2.4-jdk15.jar hello.java
  14.        
  15. java -cp .:lib/json-lib-2.4-jdk15.jar:lib/commons-lang-2.4.jar hello
  16.        
  17. commons-beanutils-1.8.0
  18. commons-collections-3.2.1
  19. commons-lang.2.5
  20. commons-logging-1.1.1
  21. ezmorph-1.0.6
  22.        
  23. xom.1.1 (if serializing from/to XML)
  24. oro-2.0.8 (if using the jdk13 version of the library)
  25.        
  26. import com.google.gson.Gson;
  27.  
  28. class Foo {
  29.     private String hello;
  30.  
  31.     public String toString() {
  32.         return "hello='" + hello + "'";
  33.     }
  34. }
  35.  
  36. public class hello {
  37.     public static void main(String[] args) {
  38.         String text = "{"hello": "world"}";
  39.  
  40.         Gson gson = new Gson();
  41.         Foo foo = gson.fromJson(text, Foo.class);
  42.  
  43.         System.out.println(foo.toString());
  44.         System.out.println(gson.toJson(foo));
  45.     }
  46. }
  47.        
  48. $ javac -cp lib/gson-2.0.jar hello.java
  49. $ java -cp .:lib/gson-2.0.jar hello
  50. hello='world'
  51. {"hello":"world"}
  52. $