当前位置:网站首页>Com alibaba. Common methods of fastjson
Com alibaba. Common methods of fastjson
2022-04-23 04:43:00 【Mount MONI】
Fastjson It's a Java library , Can be Java Object to JSON Format , Of course, it can also make JSON String conversion to Java object .
Provides toJSONString() and parseObject() Methods to Java Object and the JSON transformation . call toJSONString Fang Method to convert an object to JSON character string ,parseObject The method is reversed JSON Converts a string to an object .
Create a User object , Attributes are id,name,age,address,is_del,create_time,update_time wait
User user = new User();
user.setAddress("A1");
user.setAge(11);
user.setCreate_time(new Date());
user.setName("monishan");
1. call toJSONString Fang Method to convert an object to JSON character string
String result = toJSONStringIgnoreNullValue(user );
The output is :
{"address":"A1","age":11,"create_time":1623999789993,"name":"monishan"}
Some attribute values are null, Turn into JSON String will be ignored
add SerializerFeature.WriteMapNullValue That is to say value The value is null No display
String result = toJSONStringContainNullValue(object, SerializerFeature.WriteMapNullValue);
The output is :
{"address":"A1","age":11,"create_time":1623999789000,"del_time":null,"id":null,"is_del":null,"name":"monishan","update_time":null}
Some attributes need to be formatted , Or change value
String result = toJSONStringValueFilter(object);
Defining classes SimpleValueFilter, Realization ValueFilter Interface
public class SimpleValueFilter implements ValueFilter {
@Override
public Object process(Object object, String name, Object value) {
if (null == value) {
return "";//value The value is null Set to empty string
}
if ("name".equals(name)) {
return "***" + value + "***";
}
if ("create_time".equals(name)) {// Date formatting
return CommonUtils.dateFormat((Date)value, null);
}
return value;
}
}
The output is :
{"address":"A1","age":11,"create_time":"2021-06-18 15:03:09","del_time":"","id":"","is_del":"","name":"***monishan***","update_time":""}
2. take JSON String conversion to Java object
take json String to User user
User entity = toEntity(value, User.class);
take json String to 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}]
Encapsulation method :
/***
* <p>Title: toObject</p>
* <p>Description: take json Convert string to object of specified type , Use TypeReference You can explicitly specify the type of deserialization </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: take json Convert string to object </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: Convert object to json strand , The property value is null It doesn't show up </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: Convert object to json strand , The property value is null It will also show </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: Convert object to json strand , Rewrite the value filter to handle the serialized values uniformly according to the rules </p>
* @param <T>
* @param object
* @return
*/
public static <T> String toJSONStringValueFilter(T object) {
String result = JSONObject.toJSONString(object, new SimpleValueFilter());
return result;
}
版权声明
本文为[Mount MONI]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220558291809.html
边栏推荐
- Recommended scheme for national production of electronic components for wireless charging
- La caméra Unity tourne avec la souris
- Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000
- Case of using stream load to write data to Doris
- Supplement 14: cmake practice project notes (to be continued 4 / 22)
- 用D435i录制自己的数据集运行ORBslam2并构建稠密点云
- [paper reading] [3D target detection] point transformer
- /etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
- Recommended scheme of national manufactured electronic components
- Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
猜你喜欢

Druid -- JDBC tool class case

C language: Advanced pointer

Record your own dataset with d435i, run orbslam2 and build a dense point cloud

Bridge between ischemic stroke and intestinal flora: short chain fatty acids

Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases

520. Detect capital letters

IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
![[paper reading] [3D target detection] point transformer](/img/c5/b1fe5f206b5fe6e4dcd88dce11592d.png)
[paper reading] [3D target detection] point transformer

阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
![Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)](/img/2e/3313e563ac4f54057e359941a45098.png)
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
随机推荐
递归调用--排列的穷举
Go反射法则
Open the past and let's start over.
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
General enumeration constant class
Installation and use of Apache bench (AB pressure test tool)
C language: Advanced pointer
2019 is coming to an end, the longest day.
PIP3 installation requests Library - the most complete pit sorting
从MySQL数据库迁移到AWS DynamoDB
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
leetcode007--判断字符串中的括号是否匹配
leetcode008--实现strStr()函数
229. Find mode II
Mysql, binlog log query
Supplement: Annotation
JS generates a specified number of characters according to some words
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
Chapter 4 - understanding standard equipment documents, filters and pipelines