当前位置:网站首页>FastJson 处理泛型
FastJson 处理泛型
2022-07-26 01:08:00 【扣拉肖克钉】
阿里的 FastJson 一直都很好用,在进行对象转换映射上处理起来非常简单。但今天我才发现我一直都是在瞎用。
之前解析Map的方式是这样的:
Map map = JSON.parseObject("{'name':'zhangsan','address':'hangzhou'}", Map.class);
这样得到的 Map 是没有泛型信息的,FastJson 在无法判断的情况下默认处理为了 Map<String,Object>的格式,我们在实际使用时还需要再做一下转换。
这时我们可以通过类型参数的方式将泛型信息传入进去,这样FastJ 就能根据我们提供的泛型类型进行转换了。
String str = "{'name':'zhangsan','address':'hangzhou'}";
# 注意这里新建了一个内部类,区别在于最后有一对大括号
Map map = JSON.parseObject(str, new TypeReference<Map<String,String>>(){
});

知道这个技巧后,就能够偷很多懒了,比如这样处理:
String str = "{'name': [1,2,3,4,5]}";
Map<String, List<Integer>> map = JSON.parseObject(str, new TypeReference<Map<String, List<Integer>>>() {
});
或者由输入参数指定类型:
public static <T> void main(Class<T> clz) {
String str = "{'name': [1,2,3,4,5]}";
T instance = JSON.parseObject(str, new TypeReference<T>() {
});
}
当然,这些只是一些皮毛,如何应用还看你怎么设计了。
参考资料
边栏推荐
- Fundamentals of MATLAB shift operation
- How to switch IP and move bricks with mobile game simulator
- Gcdqueue encapsulation
- [Jizhong] July 16, 2022 1432. Oil pipeline
- 数据库系统原理与应用教程(054)—— MySQL 查询(十六):日期时间型函数的用法
- Rotate the minimum number of the array
- What if win11 cannot open its own anti-virus software? Win11's built-in anti-virus function cannot be turned on
- 【RTOS训练营】任务调度(续)、任务礼让、调度总结、队列和晚课提问
- SQL的CASE WHEN
- 《自然语言处理实战入门》深度学习基础 ---- attention 注意力机制 ,Transformer 深度解析与学习材料汇总
猜你喜欢

# 浏览器开发使用技巧

【RTOS训练营】程序框架、预习、课后作业和晚课提问

Lock upgrade: no lock, bias lock, lightweight lock, heavyweight lock

Win11打不开自带杀毒软件怎么办?win11自带杀毒功能打不开

Travel + strategy accelerated landing, jietu new product matrix exposure

Game thinking 17: Road finding engine recast and detour learning II: recast navigation grid generation process and limitations

换ip软件的用途很广及原理 动态IP更换的四种方法来保护网络隐私

REST-assured接口测试框架详解

109. Upload local files using SAP ui5 fileuploader control

The application and principle of changing IP software are very wide. Four methods of dynamic IP replacement are used to protect network privacy
随机推荐
How to copy and paste QT? (QClipboard)
SQL的CASE WHEN
pip install --upgrade can‘t find Rust compiler
【RTOS训练营】晚课学员问题
The difference and application of in and exists in SQL statement
[RTOS training camp] course learning methods and structural knowledge review + linked list knowledge
Cf1494f delete the edges (Euler circuit)
109. Upload local files using SAP ui5 fileuploader control
Paged query design of scenarios
"Introduction to natural language processing practice" deep learning foundation - attention mechanism, transformer deep analysis and learning material summary
Unityvr -- robot arm scene 4- gifts and Christmas tree
游戏思考17:寻路引擎recast和detour学习二:recast导航网格生成流程及局限性
手游多开用模拟器多开游戏如何切换IP搬砖
【RTOS训练营】设备子系统、晚课学员提问
Using questpdf operation to generate PDF is faster and more efficient!
[RTOS training camp] GPIO knowledge and preview arrangement + evening class questions
Detailed explanation of at and crontab commands of RHCE and deployment of Chrony
数据库系统原理与应用教程(055)—— MySQL 查询(十七):数学函数的用法
Unity get the animation being played
[RTOS training camp] ring buffer, at instruction, preview arrangement and evening class questions