高性能JSON框架之FastJson的简单使用

高性能JSON框架之FastJson的简单使用、

1.前言
1.1.FastJson的介绍:
JSON协议使用方便,越来越流行,JSON的处理器有很多,这里我介绍一下FastJson,FastJson是阿里的开源框架,被不少企业使用,是一个极其优秀的Json框架,Github地址: FastJson

1.2.FastJson的特点:
1.FastJson数度快,无论序列化和反序列化,都是当之无愧的fast
2.功能强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)
3.零依赖(没有依赖其它任何类库)

1.3.FastJson的简单说明:
FastJson对于json格式字符串的解析主要用到了下面三个类:
1.JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换
2.JSONObject:fastJson提供的json对象
3.JSONArray:fastJson提供json数组对象

2.FastJson的用法
首先定义三个json格式的字符串

//json字符串-简单对象型
private static final String  JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";//json字符串-数组类型
private static final String  JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";//复杂格式json字符串
private static final String  COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";

2.1.JSON格式字符串与JSON对象之间的转换
2.1.1.json字符串-简单对象型与JSONObject之间的转换

/*** json字符串-简单对象型到JSONObject的转换*/
@Test
public void testJSONStrToJSONObject() {JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);System.out.println("studentName:  " + jsonObject.getString("studentName") + ":" + "  studentAge:  "+ jsonObject.getInteger("studentAge"));}/*** JSONObject到json字符串-简单对象型的转换*/
@Test
public void testJSONObjectToJSONStr() {//已知JSONObject,目标要转换为json字符串JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);// 第一种方式String jsonString = JSONObject.toJSONString(jsonObject);// 第二种方式//String jsonString = jsonObject.toJSONString();System.out.println(jsonString);
}

2.1.2.json字符串(数组类型)与JSONArray之间的转换

/*** json字符串-数组类型到JSONArray的转换*/
@Test
public void testJSONStrToJSONArray() {JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);//遍历方式1int size = jsonArray.size();for (int i = 0; i < size; i++) {JSONObject jsonObject = jsonArray.getJSONObject(i);System.out.println("studentName:  " + jsonObject.getString("studentName") + ":" + "  studentAge:  "+ jsonObject.getInteger("studentAge"));}//遍历方式2for (Object obj : jsonArray) {JSONObject jsonObject = (JSONObject) obj;System.out.println("studentName:  " + jsonObject.getString("studentName") + ":" + "  studentAge:  "+ jsonObject.getInteger("studentAge"));}
}/*** JSONArray到json字符串-数组类型的转换*/
@Test
public void testJSONArrayToJSONStr() {//已知JSONArray,目标要转换为json字符串JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);//第一种方式String jsonString = JSONArray.toJSONString(jsonArray);// 第二种方式//String jsonString = jsonArray.toJSONString(jsonArray);System.out.println(jsonString);
}

2.1.3.复杂json格式字符串与JSONObject之间的转换

/*** 复杂json格式字符串到JSONObject的转换*/
@Test
public void testComplexJSONStrToJSONObject() {JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);String teacherName = jsonObject.getString("teacherName");Integer teacherAge = jsonObject.getInteger("teacherAge");System.out.println("teacherName:  " + teacherName + "   teacherAge:  " + teacherAge);JSONObject jsonObjectcourse = jsonObject.getJSONObject("course");//获取JSONObject中的数据String courseName = jsonObjectcourse.getString("courseName");Integer code = jsonObjectcourse.getInteger("code");System.out.println("courseName:  " + courseName + "   code:  " + code);JSONArray jsonArraystudents = jsonObject.getJSONArray("students");//遍历JSONArrayfor (Object object : jsonArraystudents) {JSONObject jsonObjectone = (JSONObject) object;String studentName = jsonObjectone.getString("studentName");Integer studentAge = jsonObjectone.getInteger("studentAge");System.out.println("studentName:  " + studentName + "   studentAge:  " + studentAge);}
}/*** 复杂JSONObject到json格式字符串的转换*/
@Test
public void testJSONObjectToComplexJSONStr() {//复杂JSONObject,目标要转换为json字符串JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);//第一种方式//String jsonString = JSONObject.toJSONString(jsonObject);//第二种方式String jsonString = jsonObject.toJSONString();System.out.println(jsonString);}

2.2.JSON格式字符串与javaBean之间的转换
2.2.1.json字符串-简单对象型与javaBean之间的转换

/*** json字符串-简单对象到JavaBean之间的转换*/
@Test
public void testJSONStrToJavaBeanObj() {//第一种方式JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);String studentName = jsonObject.getString("studentName");Integer studentAge = jsonObject.getInteger("studentAge");//Student student = new Student(studentName, studentAge);//第二种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类//Student student = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});//第三种方式,使用Gson的思想Student student = JSONObject.parseObject(JSON_OBJ_STR, Student.class);System.out.println(student);
}/*** JavaBean到json字符串-简单对象的转换*/
@Test
public void testJavaBeanObjToJSONStr() {Student student = new Student("lily", 12);String jsonString = JSONObject.toJSONString(student);System.out.println(jsonString);
}

2.2.2.json字符串-数组类型与javaBean之间的转换

/*** json字符串-数组类型到JavaBean_List的转换*/
@Test
public void testJSONStrToJavaBeanList() {//第一种方式JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);//遍历JSONArrayList<Student> students = new ArrayList<Student>();Student student = null;for (Object object : jsonArray) {JSONObject jsonObjectone = (JSONObject) object;String studentName = jsonObjectone.getString("studentName");Integer studentAge = jsonObjectone.getInteger("studentAge");student = new Student(studentName,studentAge);students.add(student);}System.out.println("students:  " + students);//第二种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类List<Student> studentList = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});System.out.println("studentList:  " + studentList);//第三种方式,使用Gson的思想List<Student> studentList1 = JSONArray.parseArray(JSON_ARRAY_STR, Student.class);System.out.println("studentList1:  " + studentList1);}/*** JavaBean_List到json字符串-数组类型的转换*/
@Test
public void testJavaBeanListToJSONStr() {Student student = new Student("lily", 12);Student studenttwo = new Student("lucy", 15);List<Student> students = new ArrayList<Student>();students.add(student);students.add(studenttwo);String jsonString = JSONArray.toJSONString(students);System.out.println(jsonString);}

2.2.3.复杂json格式字符串与与javaBean之间的转换

/*** 复杂json格式字符串到JavaBean_obj的转换*/
@Test
public void testComplexJSONStrToJavaBean(){//第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});System.out.println(teacher);//第二种方式,使用Gson思想Teacher teacher1 = JSONObject.parseObject(COMPLEX_JSON_STR, Teacher.class);System.out.println(teacher1);
}/*** 复杂JavaBean_obj到json格式字符串的转换*/
@Test
public void testJavaBeanToComplexJSONStr(){//已知复杂JavaBean_objTeacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});String jsonString = JSONObject.toJSONString(teacher);System.out.println(jsonString);
}

2.3.javaBean与json对象间的之间的转换
2.3.1.简单javaBean与json对象之间的转换

/*** 简单JavaBean_obj到json对象的转换*/
@Test
public void testJavaBeanToJSONObject(){//已知简单JavaBean_objStudent student = new Student("lily", 12);//方式一String jsonString = JSONObject.toJSONString(student);JSONObject jsonObject = JSONObject.parseObject(jsonString);System.out.println(jsonObject);//方式二JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(student);System.out.println(jsonObject1);
}/*** 简单json对象到JavaBean_obj的转换*/
@Test
public void testJSONObjectToJavaBean(){//已知简单json对象JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);//第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类Student student = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Student>() {});System.out.println(student);//第二种方式,使用Gson的思想Student student1 = JSONObject.parseObject(jsonObject.toJSONString(), Student.class);System.out.println(student1);
}

2.3.2.JavaList与JsonArray之间的转换

/*** JavaList到JsonArray的转换*/
@Test
public void testJavaListToJsonArray() {//已知JavaListStudent student = new Student("lily", 12);Student studenttwo = new Student("lucy", 15);List<Student> students = new ArrayList<Student>();students.add(student);students.add(studenttwo);//方式一String jsonString = JSONArray.toJSONString(students);JSONArray jsonArray = JSONArray.parseArray(jsonString);System.out.println(jsonArray);//方式二JSONArray jsonArray1 = (JSONArray) JSONArray.toJSON(students);System.out.println(jsonArray1);
}/*** JsonArray到JavaList的转换*/
@Test
public void testJsonArrayToJavaList() {//已知JsonArrayJSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);//第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类ArrayList<Student> students = JSONArray.parseObject(jsonArray.toJSONString(),new TypeReference<ArrayList<Student>>() {});System.out.println(students);//第二种方式,使用Gson的思想List<Student> students1 = JSONArray.parseArray(jsonArray.toJSONString(), Student.class);System.out.println(students1);
}

2.3.3.复杂JavaBean_obj与json对象之间的转换

/*** 复杂JavaBean_obj到json对象的转换*/
@Test
public void testComplexJavaBeanToJSONObject() {//已知复杂JavaBean_objStudent student = new Student("lily", 12);Student studenttwo = new Student("lucy", 15);List<Student> students = new ArrayList<Student>();students.add(student);students.add(studenttwo);Course course = new Course("english", 1270);Teacher teacher = new Teacher("crystall", 27, course, students);//方式一String jsonString = JSONObject.toJSONString(teacher);JSONObject jsonObject = JSONObject.parseObject(jsonString);System.out.println(jsonObject);//方式二JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(teacher);System.out.println(jsonObject1);}/*** 复杂json对象到JavaBean_obj的转换*/
@Test
public void testComplexJSONObjectToJavaBean() {//已知复杂json对象JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);//第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类Teacher teacher = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Teacher>() {});System.out.println(teacher);//第二种方式,使用Gson的思想Teacher teacher1 = JSONObject.parseObject(jsonObject.toJSONString(), Teacher.class);System.out.println(teacher1);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/734793.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C/C++蓝桥杯之日期问题

问题描述&#xff1a;小明正在整理一批文献&#xff0c;这些文献中出现了很多日期&#xff0c;小明知道这些日期都在1960年1月1日至2059年12月31日之间&#xff0c;令小明头疼的是&#xff0c;这些日期采用的格式非常不统一&#xff0c;有采用年/月/日的&#xff0c;有采用月/日…

蓝桥杯刷题--python-20-多路归并,贡献法

1262. 鱼塘钓鱼 - AcWing题库 nint(input()) a[0]list(map(int,input().split())) b[0]list(map(int,input().split())) l[0]list(map(int,input().split())) spend[0 for i in range(n1)] for i in range (1,n): l[i]l[i-1] tint(input()) def get(k): return max(0,a…

拿捏算法的复杂度

目录 前言 一&#xff1a;算法的时间复杂度 1.定义 2.简单的算法可以数循环的次数&#xff0c;其余需要经过计算得出表达式 3.记法&#xff1a;大O的渐近表示法 表示规则&#xff1a;对得出的时间复杂度的函数表达式&#xff0c;只关注最高阶&#xff0c;其余项和最高阶…

【音视频开发好书推荐2】《FFmpeg 音视频开发基础与实战》

1、多媒体处理开源库FFmpeg概述 享有盛名的音视频多媒体处理开源库FFmpeg&#xff0c;做过音视频编解码开发的同学基本都用过&#xff0c;即便没做过这方面开发&#xff0c;也会听说过这个开源库。 FFmpeg是目前最全面的开源音视频编解码库&#xff0c;包括常用的音视频编码协议…

JavaScript原型和原型链

JavaScript每个对象拥有一个原型对象 需要注意的是&#xff0c;只有函数对象才有 prototype 属性 当试图访问一个对象的属性时&#xff0c;它不仅仅在该对象上搜寻&#xff0c;还会搜寻该对象的原型&#xff0c;以及该对象的原型的原型&#xff0c;依次层层向上搜索&#xff…

C++指针(五)完结篇

个人主页&#xff1a;PingdiGuo_guo 收录专栏&#xff1a;C干货专栏 前言 相关文章&#xff1a;C指针&#xff08;一&#xff09;、C指针&#xff08;二&#xff09;、C指针&#xff08;三&#xff09;、C指针&#xff08;四&#xff09;万字图文详解&#xff01; 本篇博客是介…

ai学习前瞻-python环境搭建

python环境搭建 Python环境搭建1. python的安装环境2. MiniConda安装3. pycharm安装4. Jupyter 工具安装5. conda搭建虚拟环境6. 安装python模块pip安装conda安装 7. 关联虚拟环境运行项目 Python环境搭建 1. python的安装环境 ​ python环境安装有4中方式。 从上图可以了解…

Vuforia Engine 支持的操作系统、工具和设备版本

支持的版本 Vuforia Engine 支持以下操作系统、工具和设备版本,以便使用 Vuforia Engine 平台开发应用程序。 移动设备

物联网电气融合实训室建设方案

1 教学实训总体设计 1.1 建设背景 &#xff08;一&#xff09;政策推动与战略部署 近年来&#xff0c;物联网技术在全球范围内得到了广泛的关注和应用。作为信息技术的重要组成部分&#xff0c;物联网在推动经济转型升级、提升社会管理水平、改善民生福祉等方面发挥着重要作…

python实现桶排序

排序算法&#xff1a; python实现基数排序 python实现归并排序 python实现交换排序 python实现选择排序 python实现插入排序 python实现桶排序 桶排序&#xff08;Bucket Sort&#xff09;是一种排序算法&#xff0c;它将待排序的元素分到有限数量的桶&#xff08;buckets&…

Ps:清理

清理 Purge命令位于“编辑”菜单下&#xff0c;它主要用于释放 Photoshop 使用的内存资源&#xff0c;有助于提高系统的性能。 通过使用“清理”命令&#xff0c;用户可以有效管理 Photoshop 的资源使用&#xff0c;特别是在处理大型文件或进行长时间编辑会话时。 定期清理可以…

Redis持久化的两种方式

什么是Redis持久化&#xff1f; 因为redis是内存里的数据库&#xff0c;将redis中的数据保存到硬盘里就是持久化。以便在重启机器、机器故障之后恢复数据。 Redis支持两种不同的持久化操作&#xff1a; 1. 快照&#xff08;snapshotting&#xff0c;RDB&#xff09; 2. 只追…

python 基础知识点(蓝桥杯python科目个人复习计划61)

今日复习内容&#xff1a;想到什么复习什么 因为比赛用到的编辑器是IDLE&#xff0c;所以从现在开始&#xff0c;我就不用pycharm了。 例题1&#xff1a; 从1到2020的所有数字中&#xff0c;有多少个2&#xff1f; 这个题是一个填空题&#xff0c;我用的方法是先在编辑器上…

Spring篇面试题 2024

目录 Java全技术栈面试题合集地址Spring篇1.什么是“依赖注入”和“控制反转”&#xff1f;2.构造器注入和 setter 依赖注入&#xff0c;那种方式更好&#xff1f;3.什么是 Spring Framework&#xff1f;4.Spring Framework 有哪些不同的功能&#xff1f;5.Spring Framework 中…

第14章 西瓜书——概率图模型

概率图模型 概率图模型&#xff08;Probabilistic Graphical Model&#xff09;是用图结构来表示多元随机变量之间条件依赖关系的模型。在图模型中&#xff0c;节点表示随机变量&#xff0c;边表示变量之间的依赖关系。概率图模型可以分为有向图模型&#xff08;如贝叶斯网络&a…

python 给出提示,等待用户输入

在Python中&#xff0c;你可以使用内置的input()函数来提示用户输入信息。下面是一个简单的例子&#xff1a; python # 提示用户输入姓名 name input("请输入您的姓名: ")# 提示用户输入年龄&#xff0c;并将其转换为整数&#xff08;如果可能&#xff09; try:age…

实现bert训练 人工智能模型

实现BERT的训练相对复杂&#xff0c;但以下是一个简单的示例代码&#xff0c;用于使用Hugging Face库中的transformers模块在PyTorch中训练BERT模型&#xff1a; import torch from torch.utils.data import DataLoader from transformers import BertTokenizer, BertForSeque…

Oracle VM VirtualBox安装Ubuntu桌面版

背景&#xff1a;学习Docker操作 虚拟机软件&#xff1a;Oracle VM VirtualBox 7.0 系统镜像&#xff1a;ubuntu-20.04.6-desktop-amd64.iso 在Oracle VM VirtualBox新建一个虚拟电脑 选择好安装的目录和选择系统环境镜像 设置好自定义的用户名、密码、主机名 选择一下运行内…

交易平台开发:构建安全/高效/用户友好的在线交易生态圈

在数字化浪潮的推动下&#xff0c;农产品现货大宗商品撮合交易平台已成为连接全球买家与卖家的核心枢纽。随着电子商务的飞速发展&#xff0c;一个安全、高效、用户友好的交易平台对于促进交易、提升用户体验和增加用户黏性至关重要。本文将深入探讨交易平台开发的关键要素&…

Mac使用自动操作(Automator)发送文件到Android设备

需求场景 在Android开发调试的过程中&#xff0c;当需要把电脑上的文件传输到连接的Android设备时&#xff0c;通常的做法是通过adb push命令。那既然是通过命令操作&#xff0c;是否可以通过可视化的工具来操作呢&#xff1f;例如在Finder中&#xff0c;右击某一个文件或者目…