1 总结
1: JSONObject: 理解为 Map
2:JSONArray : 理解为List<Map>
3:JSON.toJSONString();会将时间转为时间戳
4:toJSONStringWithDateFormat 同一个字符串中 时间格式是统一格式
2 转成字符串
将对象变成JSON 字符串。
MyUser myUser = new MyUser();myUser.setName("小明");myUser.setTime(new Date());String s = JSON.toJSONString(myUser);System.out.println(s);//{"name":"小明","time":1724142861727}
JSONObject 变成字符串
JSONObject jsonObject = new JSONObject();jsonObject.put("name", "小明");String jsonString = jsonObject.toJSONString();System.out.println(jsonString);{"name":"小明"}
过滤器将对象转为 JSON 字符串(特殊字段转我们想要的格式)
例如 时间date转为字符串
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");MyUser myUser = new MyUser();myUser.setName("小明");myUser.setTime(new Date());ValueFilter filter = (object, name, value) -> {//object 是整个MyUser对象// MyUser(name=小明, addrList=null, time=Tue Aug 20 16:58:08 CST 2024, timeStr=null)//name 是属性名 value是属性值if (name.equals("time")) {if(value !=null){//格式话时间return sdf.format(value);}}return value;};String jsonString = JSON.toJSONString(myUser, filter);System.out.println(myUser);System.out.println(jsonString);
MyUser(name=小明, addrList=null, time=Tue Aug 20 17:02:10 CST 2024, timeStr=null)
{"name":"小明","time":"2024-08-20"}
3 字符串转成对象
字符串解析为 对象
String str="{\"name\":\"小明\"}";
MyUser myUser = JSON.parseObject(str, MyUser.class);
String str="{\"name\":\"小明\"}";
MyUser myUser2 = JSON.toJavaObject(JSON.parseObject(str), MyUser.class);
字符串解析为 对象或map (时间戳转成字符串)
serializerfeature.writemapnullvalue是Fastjson序列化器的一个特性,它表示在序列化过程中是否将Map中值为null的键值对也进行序列化输出
MyUser myUser = new MyUser();myUser.setName("小明");myUser.setTime(new Date());String s = JSON.toJSONString(myUser);System.out.println(s);Map map = JSON.parseObject(JSON.toJSONStringWithDateFormat(myUser, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue),Map.class);System.out.println(map);MyStudent myStudent = JSON.parseObject(JSON.toJSONStringWithDateFormat(myUser, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue),MyStudent.class);MyStudent myStudent = JSON.parseObject(JSON.toJSONStringWithDateFormat(myUser, "yyyy-MM-dd HH:mm:ss"),MyStudent.class);{"name":"小明","time":1724143709217}
{name=小明, time=2024-08-20 16:48:29}
字符串解析为 JSONObject
String str="{\"name\":\"小明\"}";
JSONObject jsonObject = JSON.parseObject(str);
String name = jsonObject.getString("name");
4 字符串转成集合
转成对象集合(2种方式)
ArrayList<MyUser> list = new ArrayList<>();list.add(new MyUser("小明",new Date()));list.add(new MyUser("小红",new Date()));String jsonString = JSON.toJSONString(list);List<MyUser> list2 = JSON.parseObject(jsonString, new TypeReference<List<MyUser>>() {});List<MyUser> list1 = JSON.parseObject(jsonString, List.class);System.out.println(jsonString);System.out.println(list1);
[{"name":"小明","time":1724148459440},{"name":"小红","time":1724148459440}]
[{"name":"小明","time":1724148459440}, {"name":"小红","time":1724148459440}]
转成JSONArray
ArrayList<MyUser> list = new ArrayList<>();list.add(new MyUser("小明",new Date()));list.add(new MyUser("小红",new Date()));String jsonString = JSON.toJSONString(list);JSONArray jSONArray = JSON.parseArray(jsonString);for (int i = 0; i < jSONArray.size(); i++) {JSONObject jsonObject = jSONArray.getJSONObject(i);}System.out.println(jsonString);System.out.println(jSONArray);
[{"name":"小明","time":1724148459440},{"name":"小红","time":1724148459440}]
[{"name":"小明","time":1724148459440}, {"name":"小红","time":1724148459440}]
5 字符串转成泛型
对象解析
ArrayList<MyUser> list = new ArrayList<>();list.add(new MyUser("小明",new Date()));list.add(new MyUser("小红",new Date()));Result result = new Result<List<MyUser>>();result.setData(list);String jsonString = JSON.toJSONString(result);Result<List<MyUser>> listResult = JSON.parseObject(jsonString, new TypeReference<Result<List<MyUser>>>() {});System.out.println(jsonString);System.out.println(listResult);{"data":[{"name":"小明","time":1724149088849},{"name":"小红","time":1724149088849}]}
Result(data=[MyUser(name=小明, time=Tue Aug 20 18:18:08 CST 2024), MyUser(name=小红, time=Tue Aug 20 18:18:08 CST 2024)])
集合解析
ArrayList<MyUser> list = new ArrayList<>();list.add(new MyUser("小明",new Date()));list.add(new MyUser("小红",new Date()));String jsonString = JSON.toJSONString(list);List<MyUser> list2 = JSON.parseObject(jsonString, new TypeReference<List<MyUser>>() {});System.out.println(jsonString);System.out.println(list2);
6 转字节
JSONObject jsonObject = new JSONObject();jsonObject.put("name", "小明");byte[] jsonBytes = JSON.toJSONBytes(jsonObject);String jsonString = new String(jsonBytes);System.out.println(jsonString);
7 其他
7.1检查 JSON 字符串是否合法
boolean valid = JSON.isValid(jsonString);
7.2 JSONObject api
抽出公共
String json = "{\"name\":\"小明\",\"age\":10}";JSONObject jsonObject = JSON.parseObject(json);
7.2.1 合并两个 JSON 对象属性
String jsonStr1 = "{\"name\":\"小明\"}";String jsonStr2 = "{\"age\":10}";JSONObject jsonObject1 = JSON.parseObject(jsonStr1);JSONObject jsonObject2 = JSON.parseObject(jsonStr2);jsonObject1.putAll(jsonObject2);System.out.println(jsonObject1.toJSONString());
是否包含指定的键
boolean containsName = jsonObject.containsKey("name");System.out.println(containsName );
对象中添加键值对
jsonObject.put("phone", "123-456-7890");jsonObject.putAll(additionalData);
键集合。
Set<String> keys = jsonObject.keySet();for (String key : keys) {System.out.println("Key: " + key);}
值集合
Collection<Object> values = jsonObject.values();for (Object value : values) {System.out.println("Value: " + value);}
删除指定的键
jsonObject.remove("name");
清空所有键值对。
jsonObject.clear();
检查对象是否为空
boolean empty = jsonObject.isEmpty();
检查是否包含指定的值
boolean contains = jsonObject.containsValue("小明");
如果不存在键,则向 JSON 对象添加键值对
JSONObject jsonObject = new JSONObject();jsonObject.put("name", "小明");jsonObject.putIfAbsent("age", 10); // 添加 "age" 键jsonObject.putIfAbsent("name", "小红"); // 不添加 "name" 键