当前位置:网站首页>Alibaba /热门json解析开源项目 fastjson2
Alibaba /热门json解析开源项目 fastjson2
2022-07-02 06:34:00 【Janson666】
一、介绍
FASTJSON v2是FASTJSON项目的重要升级,目标是为下一个十年提供一个高性能的JSON库。通过同一套API,
GitHub地址 : https://github.com/alibaba/fastjson2
- 支持JSON/JSONB两种协议,JSONPath是一等公民。
- 支持全量解析和部分解析。
- 支持Java服务端、客户端Android、大数据场景。
- 支持kotlin
1.使用maven添加依赖
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.3</version>
</dependency>
2. fastjson v1 兼容模块
如果原来使用fastjson 1.2.x版本,可以使用兼容包,兼容包不能保证100%兼容,请仔细测试验证,发现问题请及时反馈。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.3</version>
</dependency>
二、简单使用
1.创建实体类对象
package com.janson.demo.fastjson;
import lombok.Data;
/** * @Author Janson * @Date 2022/5/16 9:05 * @Version 1.0 */
@Data
public class DemoData {
private Integer id;
private String name;
private Integer age;
private String sex;
}
2. 测试常用的三个方法 JSON.toJSONString(JavaBean),JSON.parseObject(str, DemoData.class);JSON.parseArray(str, DemoData.class);
方法介绍:
- JSON.parseObject(str, DemoData.class) 将JSON解析为JSONObject
- JSON.parseArray(str, DemoData.class); 将JSON解析为JSONArray
- JSON.toJSONString(JavaBean),将Java对象序列化为JSON
package com.janson.demo.fastjson;
import com.alibaba.fastjson2.JSON;
import java.util.List;
/** * @Author Janson * @Date 2022/5/16 9:03 * @Version 1.0 */
public class TestFastjson {
public static void main(String[] args) {
System.out.println("-----JSON.toJSONString()-----测试javaBean转为json字符串---------------");
DemoData demoData = new DemoData();
demoData.setAge(18);
demoData.setId(11108);
demoData.setName("法外狂徒张三");
demoData.setSex("男"); //
String stoJSONString = JSON.toJSONString(demoData);
System.out.println(stoJSONString);
System.out.println("-----------测试结束--------------\n");
System.out.println("-----JSON.parseObject(s, DemoData.class);-----测试json字符串转为javaBean---------------");
DemoData demoDataparseObject = JSON.parseObject(stoJSONString, DemoData.class);
System.out.println(demoDataparseObject.getAge() + " " + demoDataparseObject.getName()+" " + demoDataparseObject.getId() + " "+demoDataparseObject.getSex());
System.out.println("-----------测试结束--------------\n");
System.out.println("-----JSON.parseArray(str, DemoData.class);-----测试一组json字符串转为javaBean的集合(list)---------------");
String str = "[{\"age\":18,\"id\":11108,\"name\":\"法外狂徒张三\",\"sex\":\"男\"},{\"age\":80,\"id\":188808,\"name\":\"法外狂徒川普\",\"sex\":\"未知\"}]";
List<DemoData> demoDataparseArray = JSON.parseArray(str, DemoData.class);
int i = 0;
for (DemoData demoDataList: demoDataparseArray) {
System.out.println("集合索引:" + i+ "集合数据" + demoDataList);
i++;
}
System.out.println("-----------测试结束--------------");
}
}
三、进阶使用 使用JSONB (JSONB 指 JSON Byte[])
3.1 将JavaBean对象序列化JSONB =>
- 方法 JSONB.toBytes(JavaBean)
- 将JSONB数据解析为JavaBean :JSONB.parseObject(bytes,DemoData.class)
System.out.println("-----JSONB.toBytes()-----测试javaBean序列化JSONB,字节数组--------------");
DemoData demoData1 = new DemoData();
demoData1.setAge(18);
demoData1.setId(11108);
demoData1.setName("法外狂徒张三");
demoData1.setSex("男");
byte[] bytes = JSONB.toBytes(demoData1);
System.out.println(bytes.length);
for (int b = 0;b<bytes.length;b++) {
System.out.println( b +" "+ bytes[b]);
}
System.out.println("-----------测试结束--------------\n");
System.out.println("-----JSONB.parseObject(bytes,DemoData.class)-----测试JSONB解析为javaBean--------------");
DemoData demoDataJSONB = JSONB.parseObject(bytes,DemoData.class);
System.out.println(demoDataJSONB.getId() + " "+ demoDataJSONB.getName() + " "
+ demoDataJSONB.getAge() + " "+demoDataJSONB.getSex());
System.out.println("-----------测试结束--------------\n");
``
边栏推荐
- Bold prediction: it will become the core player of 5g
- The channel cannot be viewed when the queue manager is running
- oracle修改数据库字符集
- Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
- Matplotlib swordsman - a stylist who can draw without tools and code
- Matplotlib剑客行——布局指南与多图实现(更新)
- Demand delineation executive summary
- Servlet全解:继承关系、生命周期、容器和请求转发与重定向等
- 一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
- How to use pyqt5 to make a sensitive word detection tool
猜你喜欢
hystrix 实现请求合并
别找了,Chrome浏览器必装插件都在这了
Microservice practice | fuse hytrix initial experience
Microservice practice | declarative service invocation openfeign practice
数构(C语言--代码有注释)——第二章、线性表(更新版)
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
Flink-使用流批一体API统计单词数量
Insight into cloud native | microservices and microservice architecture
Micro service practice | introduction and practice of zuul, a micro service gateway
随机推荐
Ora-12514 problem solving method
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
Cartoon rendering - average normal stroke
C4D quick start tutorial - Chamfer
Thinkphp5 how to determine whether a table exists
idea查看字节码配置
Who is better for Beijing software development? How to find someone to develop system software
Learn combinelatest through a practical example
[go practical basis] how can gin get the request parameters of get and post
Redis installation and deployment (windows/linux)
微服务实战|原生态实现服务的发现与调用
Redis zadd导致的一次线上问题排查和处理
Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别
概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
【Go实战基础】gin 高效神器,如何将参数绑定到结构体
Redis sorted set data type API and application scenario analysis
Chrome browser plug-in fatkun installation and introduction
Enterprise level SaaS CRM implementation
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method