JSONObject对象
底层是对Map<Strng, Object> 的封装:
提示:JSONObject通常处理前端传递的数据格式为对象 - post请求
{ name: "lxc", age: 20, address: "山东", say: [1, 2, 4], em: { a: "1", b: "2" } }
常用方法:
以下测试的前端数据格式为:{ delIds: [1, 2, 11, 12, 3] }
(1)Object get(String key)
通过键获取值,获取到的值是Object类型的。
@PostMapping(value = "/deleteIds")
public void deleteIds(@RequestBody JSONObject jsonObject) {
Object key = jsonObject.get("delIds");
System.out.println(JSON.toJSONString(key)); // [1,2,11,12,3]
}
补充:
获取String类型的值
// 前端传过来的 {name: "lxc"}
@PostMapping(value = "/deleteIds")
public void deleteIds(@RequestBody JSONObject jsonObject) {
String name = jsonObejct.getString("name") / "lxc"
}
getString() 方法底层源码还是调用的get()
(2)int size()
获取JSONObject对象种键值对儿的数量。
@PostMapping(value = "/deleteIds")
public void deleteIds(@RequestBody JSONObject jsonObject) {
int count = jsonObject.size();
System.out.println(count); // 1
}
(3)boolean isEmpty()
判断该JSONObject对象是否为空。
@PostMapping(value = "/deleteIds")
public void deleteIds(@RequestBody JSONObject jsonObject) {
boolean isEmpty = jsonObject.isEmpty();
System.out.println(isEmpty); // false
}
(4)boolean containsKey(String key)
判断是否包含指定的key
@PostMapping(value = "/deleteIds")
public void deleteIds(@RequestBody JSONObject jsonObject) {
boolean hasKey = jsonObject.containsKey("delIds");
System.out.println(hasKey); // true
}
(5)JSONObject getJSONObject(String key)
如果JSONObject对象中的value值是一个JSONObject对象,那么可以根据key获取这个JSONObject对象。
对应前端传的格式为:{ delIds: {name: "测试"} }
@PostMapping(value = "/deleteIds")
public void deleteIds(@RequestBody JSONObject jsonObject) {
JSONObject obj = jsonObject.getJSONObject("delIds");
System.out.println(JSON.toJSONString(obj)); // {"name":"测试"}
}
(6)JSONObject getJSONArray(String key)
如果JSONObject对象中的value值是一个JSONObject数组,那么可以根据key获取这个JSONObject数组。
对应前端传的格式为:{ delIds: [1, 2, 11, 12, 3] }
@PostMapping(value = "/deleteIds")
public void deleteIds(@RequestBody JSONObject jsonObject) {
System.out.println(JSON.toJSONString((jsonObject))); // {"delIds":[1,2,11,12,3]}
}
(7)Object remove(Object key)
根据key删除某个键值对,返回删除成功后的key对应的value值,原数据会被改变。
对应前端传过来的数据为: { delIds: {name: "测试"}, test: "hehe" }
@PostMapping(value = "/deleteIds")
public void deleteIds(@RequestBody JSONObject jsonObject) {
Object filterObj = jsonObject.remove("test");
System.out.println(JSON.toJSONString(filterObj)); // "hehe"
System.out.println(JSON.toJSONString(jsonObject)); // {"delIds":{"name":"测试"}}
}
(8)Set<String> keySet()
获取jsonObject对象中的所有key,并将其放入Set集合中。
@PostMapping(value = "/deleteIds")
public void deleteIds(@RequestBody JSONObject jsonObject) {
Set<String> keySet = jsonObject.keySet();
System.out.println(JSON.toJSONString(keySet)); // ["test","delIds"]
}
(9) Set<Map.Entry<String, Object>> entrySet()
在循环时使用,获取的结果为:由键值对组成的对象。
对应前端传过来的数据格式为:{ delIds: { name: "测试" }, test: "hehe" }
@PostMapping(value = "/deleteIds")
public void deleteIds(@RequestBody JSONObject jsonObject) {
Set<Map.Entry<String, Object>> keySet = jsonObject.entrySet();
for (Map.Entry<String, Object> item : keySet) {
System.out.println(JSON.toJSONString(item)); //输出结果: {"test":"hehe"} 和 {"delIds":{"name":"测试"}}
}
}