標籤:json-lib stat 引用 artifact element nbsp style 數組 字串
如下是我所用json第三方jar包的maven座標:
<!--可引用的jar--><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier></dependency><!--源碼--><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15-sources</classifier></dependency>
View Code
JSONObject中常用的方法說明:
1 package cn.json; 2 3 4 import net.sf.json.JSONObject; 5 6 public class JsonDemo { 7 private JSONObject json=new JSONObject(); 8 9 10 public static void main(String[] args) {11 new JsonDemo().put();12 /**<結果>13 * 對null對象的插入14 * {"accumulate":null}15 * 對null字串的插入16 * {"accumulate":[null,null],"element":null,"put":null}17 * 對字串的插入18 * {"accumulate":[null,null,""],"element":"","put":""}19 * */ 20 } 21 22 /**23 * json的插入方法比較24 * 1.accumulate方法,可以對null對象插入,25 * 插入多個相同key的value值時,value將以插入順序數組排列26 * 2.element 和 put 方法 插入 null對象 27 * 則這對key-value消失,插入多個相同的key-value,最終只保留最後一個28 */29 public void put(){30 System.out.println("對null對象的插入");31 String value=null;32 json.element("element",value);33 json.accumulate("accumulate", value);34 json.put("put", value);35 System.out.println(json);36 System.out.println("對null字串的插入");37 value="null";38 json.element("element",value);39 json.accumulate("accumulate", value);40 json.put("put", value);41 System.out.println(json);42 System.out.println("對"+""+"字串的插入");43 value="";44 json.element("element",value);45 json.accumulate("accumulate", value);46 json.put("put", value);47 System.out.println(json);48 49 };50 51 }View Code
JSON JAVA 總結