当前位置:网站首页>com.alibaba.fastjson 常用方法
com.alibaba.fastjson 常用方法
2022-04-22 06:00:00 【莫尼山】
Fastjson 是一个 Java 库,可以将 Java 对象转换为 JSON 格式,当然它也可以将 JSON 字符串转换为 Java 对象。
提供了 toJSONString() 和 parseObject() 方法来将 Java 对象与 JSON 相互转换。调用toJSONString方 法即可将对象转换成 JSON 字符串,parseObject 方法则反过来将 JSON 字符串转换成对象。
创建一个User对象,属性有id,name,age,address,is_del,create_time,update_time等等
User user = new User();
user.setAddress("A1");
user.setAge(11);
user.setCreate_time(new Date());
user.setName("monishan");
1.调用toJSONString方 法即可将对象转换成 JSON 字符串
String result = toJSONStringIgnoreNullValue(user );
输出结果为:
{"address":"A1","age":11,"create_time":1623999789993,"name":"monishan"}
一些属性值为null,转成JSON字符串会忽略掉
加上SerializerFeature.WriteMapNullValue即可实现value值为null不显示
String result = toJSONStringContainNullValue(object, SerializerFeature.WriteMapNullValue);
输出结果为:
{"address":"A1","age":11,"create_time":1623999789000,"del_time":null,"id":null,"is_del":null,"name":"monishan","update_time":null}
对某些属性需要格式化,或改变value
String result = toJSONStringValueFilter(object);
定义类SimpleValueFilter,实现ValueFilter接口
public class SimpleValueFilter implements ValueFilter {
@Override
public Object process(Object object, String name, Object value) {
if (null == value) {
return "";//value值为null置为空串
}
if ("name".equals(name)) {
return "***" + value + "***";
}
if ("create_time".equals(name)) {//日期格式化
return CommonUtils.dateFormat((Date)value, null);
}
return value;
}
}
输出结果为:
{"address":"A1","age":11,"create_time":"2021-06-18 15:03:09","del_time":"","id":"","is_del":"","name":"***monishan***","update_time":""}
2.将 JSON 字符串转换为 Java 对象
将json串转为User user
User entity = toEntity(value, User.class);
将json串转为List<User> userList
List<User> userList = toObject(value, new com.alibaba.fastjson.TypeReference<List<User>>() {});
[{"address":"A1","age":11,"create_time":1624003599005,"del_time":null,"id":null,"is_del":null,"name":"monishan","update_time":null}]
封装方法:
/***
* <p>Title: toObject</p>
* <p>Description: 将json串转为指定类型对象,使用TypeReference可以明确的指定反序列化的类型</p>
* @param <T>
* @param value
* @param typeReference
* @return
*/
public static <T> T toObject(String value, com.alibaba.fastjson.TypeReference<T> typeReference) {
return JSONObject.parseObject(value, typeReference, Feature.values());
}
/***
* <p>Title: toEntity</p>
* <p>Description: 将json串转为对象</p>
* @param <T>
* @param value
* @param clazz
* @return
*/
public static <T> T toEntity(String value, Class<T> clazz) {
return JSONObject.parseObject(value, clazz);
}
/***
* <p>Title: toJSONStringIgnoreNullValue</p>
* <p>Description: 将对象转为json串,属性值为null则不会显示</p>
* @param <T>
* @param object
* @return
*/
public static <T> String toJSONStringIgnoreNullValue(T object) {
String result = JSONObject.toJSONString(object);
return result;
}
/***
* <p>Title: toJSONStringContainNullValue</p>
* <p>Description: 将对象转为json串,属性值为null也会显示出来</p>
* @param <T>
* @param object
* @return
*/
public static <T> String toJSONStringContainNullValue(T object) {
String result = JSONObject.toJSONString(object, SerializerFeature.WriteMapNullValue);
return result;
}
/***
* <p>Title: toJSONStringValueFilter</p>
* <p>Description: 将对象转为json串,重写值过滤器按照规则来对序列化出来的值做统一处理</p>
* @param <T>
* @param object
* @return
*/
public static <T> String toJSONStringValueFilter(T object) {
String result = JSONObject.toJSONString(object, new SimpleValueFilter());
return result;
}
版权声明
本文为[莫尼山]所创,转载请带上原文链接,感谢
https://blog.csdn.net/shy415502155/article/details/118026595
边栏推荐
- 创新实训(五)中期检查
- MongoDB初接触——基础介绍篇
- ArcGIS 观景点视域分析
- JS five methods to judge the most complete data types
- uniapp全局拦截401跳转到登录页
- The difference between watch and computed
- 封装微信小程序实时获取经纬度+权限判定+引导授权(无30秒延迟)
- Uniapp custom bottom navigation bar H5 has an effective solution to invalid applet
- js引擎的循环机制:同步,异步,事件循环
- Uniapp global interception 401 jumps to the login page
猜你喜欢

Cancel password after excel worksheet forgets password

一套sql语句同时支持Oracle跟Mysql?

COM in Wine(2)——基本代码分析

创新实训(九)整合

Uniapp custom bottom navigation bar H5 has an effective solution to invalid applet

关于一段小程序的思考

The minors protection solution of digital beauty technology is heavily launched, opening a new era of minors' network escort

EXCEL 分列功能的使用

OLAP和OLTP的区别及所对应模型(基础知识)

Click to trigger other DOM elements: < $refs, $El >
随机推荐
Automatically add partitions for PostgreSQL tables
Waterfall layout JS positioning
The display of redis stored data is garbled
A set of SQL statements supports both Oracle and MySQL?
Iframe子父级传参
一套sql语句同时支持Oracle跟Mysql?
创新实训(九)整合
ArcGIS 观景点视域分析
pixel手机救砖教程
创新实训(六)路由
A series of interpretations of the general data protection regulations (gdpr): how to judge whether offshore enterprises are under the jurisdiction of gdpr?
PostgreSQL 13.3, 12.7, 11.12, 10.17 and 9.6.22 have been released!
Mapping of oid and relfilenode in PostgreSQL
Solve the problem of error in installing PostgreSQL under windows2012 R2
How can enterprise risk control build four systems to achieve overall prevention and control?
EXCEL IFS函数的使用
Cancel password after excel worksheet forgets password
EXCEL 数据透视表的简单使用
从零开始学安卓(kotlin)一 ——入门
windows远程连接redis