- 1 package cn.json;
- 2
- 3
- 4 import net.sf.json.JSONObject;
- 5
- 6 public class JsonDemo {
- 7 privateJSONObject 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(){
- 30System.out.println("对null对象的插入");
- 31String value=null;
- 32json.element("element",value);
- 33json.accumulate("accumulate", value);
- 34json.put("put", value);
- 35 System.out.println(json);
- 36System.out.println("对null字符串的插入");
- 37value="null";
- 38json.element("element",value);
- 39json.accumulate("accumulate", value);
- 40json.put("put", value);
- 41 System.out.println(json);
- 42System.out.println("对"+""+"字符串的插入");
- 43value="";
- 44json.element("element",value);
- 45json.accumulate("accumulate", value);
- 46json.put("put", value);
- 47 System.out.println(json);
- 48
- 49 };
- 50
- 51}
来源: http://www.bubuko.com/infodetail-1990923.html