JSONObject中的put、accumulate和element的区别
实体类:
public class Student {
private String name;
private int age;
private List<String> firends;
...
}
accumulate操作:添加一个键值对,当在同一个键下accumulate添加多次,会增加这个键的值,不会覆盖
累积value到这个key下。这个方法同element()方法类似,特殊的是,如果当前已经存在一个
value在这个key下那么一个JSONArray将会存储在这个key下来保存所有累积的value。如果已
经存在一个JSONArray,那么当前的value就会添加到这个JSONArray中。
@Test
public void test1() {
ArrayList<String> list = new ArrayList<>();
list.add("jack");
list.add("tom");
list.add("lucy");
Student rose = new Student("rose", 20,list);
//javabean to jsonObject
JSONObject jsonOb = JSONObject.fromObject(rose);
System.out.println(jsonOb);
//{"name":"rose","firends":["jack","tom","lucy"],"age":20}
//jsonObject的操作:accumulate添加一个键值对,如下
jsonOb.accumulate("hello", "good!");
System.out.println(jsonOb);
//{"name":"rose","firends":["jack","tom","lucy"],"age":20,"hello":"good!"}
//当accumulate方法的执行多次的键是相同的,那么会往这个键下增加元素,不会覆盖,如下:
jsonOb.accumulate("hello", "good!");
System.out.println(jsonOb);
//{"name":"rose","firends":["jack","tom","lucy"],"age":20,"hello":["good!","good!"]}
jsonOb.accumulate("hello", null);
System.out.println(jsonOb);
//{"name":"rose","firends":["jack","tom","lucy"],"age":20,"hello":["good!","good!",null]}
jsonOb.accumulate("hello", JSONNull.getInstance());
System.out.println(jsonOb);
//{"name":"rose","firends":["jack","tom","lucy"],"age":20,"hello":["good!","good!",null,null]}
}
put操作:增加一个键值对,往一个键下再次使用put操作,会覆盖原来的值
将value映射到key下。如果此JSONObject对象之前存在一个value在这个key下,当前的value
会替换掉之前的value。当值null时移除该键,当键为null时,会报错。
@Test
public void test3() {
ArrayList<String> list = new ArrayList<>();
list.add("jack");
list.add("tom");
list.add("lucy");
Student rose = new Student("rose", 20,list);
//javabean to jsonObject
JSONObject jsonOb = JSONObject.fromObject(rose);
System.out.println(jsonOb);
//{"name":"rose","firends":["jack","tom","lucy"],"age":20}
//jsonObject的操作:put
jsonOb.put("hello", "good!");
System.out.println(jsonOb);
//{"name":"rose","firends":["jack","tom","lucy"],"age":20,"hello":"good!"}
jsonOb.put("hello", "goodmorning!");
System.out.println(jsonOb);
//{"name":"rose","firends":["jack","tom","lucy"],"age":20,"hello":"goodmorning!"}
jsonOb.put("firends", null);
System.out.println(jsonOb);
//{"name":"rose","firends":["jack","tom","lucy"],"age":20}
}
element操作:这个官方文档中说明个人感觉是不准确的,个人测试:实际相当于一个put方法,但是值不能为null。可以为JSONNull.getinstance.而put的值是可以为null和JSONNull.getinstance。当为null时移除该键。
@Test
public void test4() {
ArrayList<String> list = new ArrayList<>();
list.add("jack");
list.add("tom");
list.add("lucy");
Student rose = new Student("rose", 20,list);
//javabean to jsonObject
JSONObject jsonOb = JSONObject.fromObject(rose);
System.out.println(jsonOb);
System.out.println("-------------------------------------------------");
//{"name":"rose","firends":["jack","tom","lucy"],"age":20}
//jsonObject的操作:element
jsonOb.element("firends",JSONNull.getInstance());
System.out.println(jsonOb);
System.out.println("-------------------------------------------------");
jsonOb.put("firends", null);
System.out.println(jsonOb);
}
有不对请指点!