先来看下它有哪些常用方法,以及有什么作用:
1.put(String key, Object value)方法,在JSONObject对象中设置键值对在,在进行设值得时候,key是唯一的,如果用相同的key不断设值得时候,保留后面的值。
2.Object get(String key) :根据key值获取JSONObject对象中对应的value值,获取到的值是Object类型,需要手动转化为需要的数据类型
3.int size():获取JSONObject对象中键值对的数量
4.boolean isEmpty():判断该JSONObject对象是否为空
5.containsKey(Object key):判断是否有需要的key值
6.boolean containsValue(Object value):判断是否有需要的value值
7.JSONObject getJSONObject(String key):如果JSONObjct对象中的value是一个JSONObject对象,即根据key获取对应的JSONObject对象;
8.JSONArray getJSONArray(String key) :如果JSONObject对象中的value是一个JSONObject数组,既根据key获取对应的JSONObject数组;
9.Object remove(Object key):根据key清除某一个键值对。
由于JSONObject是一个map,它还具有map特有的两个方法:
10.Set
11.Set<Map.Entry<String, Object>> entrySet():在循环遍历时使用,取得是键和值的映射关系,Entry就是Map接口中的内部接口
与String字符串转换:
12.toJSONString() /toString():将JSONObject对象转换为json的字符串
常用的方法主要为以上这些,下面列出使用这些方法的example:
package com.snake.test; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * @Author Snake * @Date 2019/11/24 14:21 * @Version 1.0.0 */ public class myTest { public static void main(String[] args) { Map<String, Object> map = new HashMap<>(); map.put("username","snake"); map.put("age","18"); map.put("password","123456"); JSONObject jsonObject = new JSONObject(map); //jsonObject:{"password":"123456","age":"18","username":"snake"} System.out.println("jsonObject:"+jsonObject); jsonObject.remove("age"); //去除一个key-values后的jsonObject:{"password":"123456","username":"snake"} System.out.println("去除一个key-values后的jsonObject:"+jsonObject); Object put = jsonObject.put("username", "snake17"); //jsonObject.put:snake System.out.println("jsonObject.put:"+put); Object put1 = jsonObject.put("username", "snake27"); //第一次jsonObject.put:snake17 System.out.println("第一次jsonObject.put:"+put1); //第二次jsonObject.put:snake17 System.out.println("第二次jsonObject.put:"+put1); JSONObject jsonObject1 = jsonObject.fluentPut("username", "snake107"); //jsonObject1.fluentPut:{"password":"123456","username":"snake107"} System.out.println("jsonObject1.fluentPut:"+jsonObject1); Set<Map.Entry<String, Object>> entries = jsonObject.entrySet(); //set键值对关系:[password=123456, username=snake107] System.out.println("set键值对关系:"+entries); Map<String, Object> innerMap = jsonObject.getInnerMap(); //map对象:{password=123456, username=snake107} System.out.println("map对象:"+innerMap); Set<String> strings = jsonObject.keySet(); //key值:[password, username] System.out.println("key值:"+strings); User user = jsonObject.toJavaObject(User.class); //jsonObject转对象:User(username=snake107, age=null, password=123456) System.out.println("jsonObject转对象:"+user); String s = jsonObject.toJSONString(); //jsonObject转jsonString{"password":"123456","username":"snake107"} System.out.println("jsonObject转jsonString"+s); Object parse = JSONObject.parse(s); //jsonString转object:{"password":"123456","username":"snake107"} System.out.println("jsonString转object:"+parse); User user1 = JSONObject.parseObject(s, User.class); //jsonString转对象:User(username=snake107, age=null, password=123456) System.out.println("jsonString转对象:"+user1); ArrayList<User> users = new ArrayList<>(); users.add(user); Object usersObject = JSONArray.parse(JSONArray.toJSONString(users)); //usersObject:[{"password":"123456","username":"snake107"}] System.out.println("usersObject:"+usersObject); String s1 = JSONObject.toJSONString(usersObject); ArrayList arrayList = JSONObject.parseObject(s1, users.getClass()); //[{"password":"123456","username":"snake107"}] System.out.println(arrayList); JSONArray objects = JSONArray.parseArray(JSONArray.toJSONString(users)); //jsonArray:[{"password":"123456","username":"snake107"}] System.out.println("jsonArray:"+objects); } }