当前位置:网站首页>Fastjason handles generics
Fastjason handles generics
2022-07-26 01:17:00 【Buckle shocker nail】
Ali's FastJson It's always good to use , It is very simple to deal with object transformation mapping . But today I found that I have been using it blindly .
Resolve before Map This is the way :
Map map = JSON.parseObject("{'name':'zhangsan','address':'hangzhou'}", Map.class);
That's what we got Map There is no generic information ,FastJson If it is impossible to judge, the default processing is Map<String,Object> The format of , We still need to do some conversion in actual use .
At this time, we can pass in generic information through type parameters , such FastJ You can convert according to the generic type we provide .
String str = "{'name':'zhangsan','address':'hangzhou'}";
# Notice that an inner class is created here , The difference is that there is a pair of braces at the end
Map map = JSON.parseObject(str, new TypeReference<Map<String,String>>(){
});

After knowing this skill , You can steal a lot of laziness , Like this :
String str = "{'name': [1,2,3,4,5]}";
Map<String, List<Integer>> map = JSON.parseObject(str, new TypeReference<Map<String, List<Integer>>>() {
});
Or the type is specified by the input parameter :
public static <T> void main(Class<T> clz) {
String str = "{'name': [1,2,3,4,5]}";
T instance = JSON.parseObject(str, new TypeReference<T>() {
});
}
Of course , These are just fur , How to apply depends on how you design .
Reference material
FastJson Parsing multilevel generics
fastjson Use generics to convert objects
fastJson Deserialization handles generics
边栏推荐
- 中心对称的二进制模式CSLBP,matlab
- 全国一半人跑长沙,长沙一半人跑哪?
- # 浏览器开发使用技巧
- Pycharm automatically adds header comments when creating py files
- FreeBSD bnxt以太网驱动源码阅读记录二:
- Matlab bitwise and or not
- 数据库系统原理与应用教程(056)—— MySQL 查询(十八):其他类型函数的用法
- Linked list related interview questions
- 1.30 升级bin文件添加后缀及文件长度
- [CTF] crypto preliminary basic outline
猜你喜欢
随机推荐
Inverse matrix block matrix
Tutorial on the principle and application of database system (057) -- MySQL exercises
[software development specification iv] application system security coding specification
matlab 按位 与 或 非
力扣 25. K 个一组翻转链表
How accurate is the IP address? What are dynamic IP and static IP? The most common method of switching IP
链表相关面试题
ZK-Rollups工作原理
第二届中国Rust开发者大会来啦,完整议程大曝光!
NLP introduction + practice: Chapter 3: gradient descent and back propagation
Embedded development: tips and tricks -- seven tips for designing powerful boot loader
"Yuanqi Cola" is not the end point, "China Cola" is
Spine_ Adnexal skin
Handler消息机制-FWK层
【ctf】Crypto初步基础概要
[software development specification II] prohibited item development specification
Working principle of ZK rollups
How to switch IP and move bricks with mobile game simulator
更换IP地址常见的4种简单有效的方法
Fundamentals of MATLAB shift operation









