org.json与json-lib的区别

来源:互联网 发布:淘宝网页无法登陆 编辑:程序博客网 时间:2024/05/01 22:16
//在www.json.org上公布了很多JAVA下的json解析工具,其中org.json和json-lib比较简单,两者使用上差不多。下面两段源代码是分别使用这两个工具解析和构造JSON的演示程序。 //这是使用json-lib的程序: import JAVA.util.HashMap; import JAVA.util.Map;   import net.sf.json.JSONObject;   public class Test {       public static void main(String[] args) {         String json = "{\"name\":\"reiz\"}";         JSONObject jsonObj = JSONObject.fromObject(json);         String name = jsonObj.getString("name");                jsonObj.put("inITial", name.substring(0, 1).toUpperCASE());           String[] likes = new String[] { "JAVAScript", "Skiing", "Apple Pie" };         jsonObj.put("likes", likes);           Map <String, String> ingredients = new HashMap <String, String>();         ingredients.put("apples", "3kg");         ingredients.put("sugar", "1kg");         ingredients.put("pastry", "2.4kg");         ingredients.put("bestEaten", "outdoors");         jsonObj.put("ingredients",ingredients);                System.out.println(jsonObj);     } } //这是使用org.json的程序: import JAVA.util.HashMap; import JAVA.util.Map;   import org.json.JSONException; import org.json.JSONObject;   public class Test {       public static void main(String[] args) throws JSONException {         String json = "{\"name\":\"reiz\"}";         JSONObject jsonObj = new JSONObject(json);         String name = jsonObj.getString("name");           jsonObj.put("inITial", name.substring(0, 1).toUpperCASE());           String[] likes = new String[] { "JAVAScript", "Skiing", "Apple Pie" };         jsonObj.put("likes", likes);           Map <String, String> ingredients = new HashMap <String, String>();         ingredients.put("apples", "3kg");         ingredients.put("sugar", "1kg");         ingredients.put("pastry", "2.4kg");         ingredients.put("bestEaten", "outdoors");         jsonObj.put("ingredients", ingredients);         System.out.println(jsonObj);           System.out.println(jsonObj);     } } //两者的使用几乎是相同的,但org.json比json-lib要轻量得多,前者没有任何依赖,而后者要依赖ezmorph和commons的lang、logging、beanutils、collections等组件。


 

 

转载:http://www.cnblogs.com/zero0/articles/1874615.html

原创粉丝点击