1.如下是我所用json第三方jar包的maven坐标
1 <!--可引用的jar-->
2 <dependency>
3 <groupId>net.sf.json-lib</groupId>
4 <artifactId>json-lib</artifactId>
5 <version>2.4</version>
6 <classifier>jdk15</classifier>
7 </dependency>
8 <!--源码-->
9 <dependency>
10 <groupId>net.sf.json-lib</groupId>
11 <artifactId>json-lib</artifactId>
12 <version>2.4</version>
13 <classifier>jdk15-sources</classifier>
14 </dependency>
2.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 }