当前位置:网站首页>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");
``
边栏推荐
- Matplotlib剑客行——没有工具用代码也能画图的造型师
- Amq6126 problem solving ideas
- Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程
- 概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
- In depth analysis of how the JVM executes Hello World
- JVM指令助记符
- AMQ 4043 solution for errors when using IBM MQ remote connection
- View the port of the application published by was
- Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
- A detailed explanation takes you to reproduce the statistical learning method again -- Chapter 2, perceptron model
猜你喜欢

企业级SaaS CRM实现

别找了,Chrome浏览器必装插件都在这了

Don't look for it. All the necessary plug-ins for Chrome browser are here
![[go practical basis] how to bind and use URL parameters in gin](/img/63/84717b0da3a55d7fda9d57c8da2463.png)
[go practical basis] how to bind and use URL parameters in gin

Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method

【Go实战基础】gin 如何自定义和使用一个中间件

idea查看字节码配置

Microservice practice | declarative service invocation openfeign practice

Servlet全解:继承关系、生命周期、容器和请求转发与重定向等

概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
随机推荐
How to use pyqt5 to make a sensitive word detection tool
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
VIM操作命令大全
[go practical basis] how to verify request parameters in gin
聊聊消息队列高性能的秘密——零拷贝技术
Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
破茧|一文说透什么是真正的云原生
微服务实战|微服务网关Zuul入门与实战
[go practical basis] how to bind and use URL parameters in gin
MySQL multi column in operation
Troubleshooting and handling of an online problem caused by redis zadd
Chrome视频下载插件–Video Downloader for Chrome
Knife4j 2.X版本文件上传无选择文件控件问题解决
双非本科生进大厂,而我还在底层默默地爬树(上)
Thinkphp5 how to determine whether a table exists
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
Matplotlib swordsman line - layout guide and multi map implementation (Updated)
【Go实战基础】gin 如何验证请求参数
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
Learn combinelatest through a practical example