高性能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);
}
相关文章:
高性能JSON框架之FastJson的简单使用
高性能JSON框架之FastJson的简单使用、 1.前言 1.1.FastJson的介绍: JSON协议使用方便,越来越流行,JSON的处理器有很多,这里我介绍一下FastJson,FastJson是阿里的开源框架,被不少企业使用,是一个极其优秀的Json框架,Github地址: FastJson 1.2.FastJson的特点: 1.F…...
★判断素数的几种方法(由易到难,由慢到快)
素数的定义: 素数,又称为质数,指的是“大于1的整数中,只能被1和这个数本身整除的数”。换句话说,素数是只有两个正约数(1和本身)的自然数。素数在数论中有着重要的地位,且素数的个数…...

vue svelte solid 虚拟滚动性能对比
前言 由于svelte solid 两大无虚拟DOM框架,由于其性能好,在前端越来越有影响力。 因此本次想要验证,这三个框架关于实现表格虚拟滚动的性能。 比较版本 vue3.4.21svelte4.2.12solid-js1.8.15 比较代码 这里使用了我的 stk-table-vue(np…...

IDEA中新增文件,弹出框提示是否添加到Git点错了,怎么重新设置?
打开一个配置了Git的项目,新增一个文件,会弹出下面这个框。提示是否将新增的文件交给Git管理。 一般来说,会选择ADD,并勾选Dont ask agin,添加并不再询问。如果不小心点错了,可在IDEA中重新设置(…...
LV15 day5 字符设备驱动读写操作实现
一、读操作实现 ssize_t xxx_read(struct file *filp, char __user *pbuf, size_t count, loff_t *ppos); 完成功能:读取设备产生的数据 参数: filp:指向open产生的struct file类型的对象,表示本次read对应的那次open pbuf&#…...

Uninty 鼠标点击(摄像机发出射线-检测位置)
平面来触发碰撞,胶囊用红色材质方便观察。 脚本挂载到胶囊上方便操作。 目前实现的功能,鼠标左键点击,胶囊就移动到那个位置上。 using System.Collections; using System.Collections.Generic; using UnityEngine;public class c6 : MonoBe…...
描述下Vue自定义指令
描述下Vue自定义指令 (1)自定义指令基本内容(2)使用场景(3)使用案例 在 Vue2.0 中,代码复用和抽象的主要形式是组件。然而,有的情况下,你仍然需要对普通 DOM 元素进行底层…...
2024.3.7
作业: 1、OSI的七层网络模型有哪些,每一层有什么作用? (1)应用层 负责处理不同应用程序之间的通信,需要满足提供的协议,确保数据发送方和接收方的正确 (2)表示层…...
this.$watch 侦听器 和 停止侦听器
使用组件实例的$watch()方法来命令式地创建一个侦听器; 它还允许你提前停止该侦听器 语法:this.$watch(data, method, object) 1. data:侦听的数据源,类型为String 2. method:回调函数&#x…...
P1030 [NOIP2001 普及组] 求先序排列题解
题目 给出一棵二叉树的中序与后序排列。求出它的先序排列。(约定树结点用不同的大写字母表示,且二叉树的节点个数≤8)。 输入输出格式 输入格式 共两行,均为大写字母组成的字符串,表示一棵二叉树的中序与后序排列。…...
【分布式】NCCL Split Tree kernel内实现情况 - 06
相关系列 【分布式】NCCL部署与测试 - 01 【分布式】入门级NCCL多机并行实践 - 02 【分布式】小白看Ring算法 - 03 【分布式】大模型分布式训练入门与实践 - 04 目录 相关系列概述1.1 Tree1.2 double binary tree初始化和拓扑2.1 Tree的初始化与差异2.2 ncclGetBtreeKernel内部…...
C语言深入学习 --- 4.自定义类型(结构体+枚举+联合)
第四章 自定义类型:结构体,枚举,联合 结构体 结构体类型的声明 结构的自引用 结构体变量的定义和初始化 结构体的内存对齐 结构体实现位段(位段的填充 和 可移植性) 枚举 枚举类型的定义 枚举的优点 枚举的使…...
AI自然语言中默认上下文长度4K 几K是什么意思?
环境: 4K 问题描述: AI自然语言中默认上下文长度4K 几K是什么意思? 解决方案: 在自然语言处理中,“k” 表示 “千”,是一种简写方式。当我们说 “4k” 时,实际上指的是 “4,000”。在上下文…...

vSphere 8考试认证题库 2024最新(VCP 8.0版本)
VMware VCP-DCV(2V0-21.23)认证考试题库,已全部更新,答案已经完成校对,完整题库请扫描上方二维码访问。正常考可以考到450分以上(满分500分,300分通过) An administrator is tasked …...
系统学习Python——装饰器:“私有“和“公有“属性案例-[装饰器参数、状态保持和外层作用域]
分类目录:《系统学习Python》总目录 文章《系统学习Python——装饰器:“私有“和“公有“属性案例-[实现私有属性]》中使用的类装饰器接受任意多个参数来命名私有属性。然而真正发生的情况是,参数传递给了Private函数,然后Private…...

星辰天合参与编制 国内首个可兼顾 AI 大模型训练的高性能计算存储标准正式发布
近日,在中国电子工业标准化技术协会高标委的支持和指导下,XSKY星辰天合作为核心成员参与编制的《高性能计算分布式存储系统技术要求》团体标准,在中国电子工业标准化技术协会网站正式发布。 该团体标准强调了分布式存储系统对包括传统高性能计…...
算法训练day38动态规划基础Leetcode509斐波纳切数70爬楼梯746使用最小花费爬楼梯
什么是动态规划 对于动态规划问题,我将拆解为如下五步曲,这五步都搞清楚了,才能说把动态规划真的掌握了! 确定dp数组(dp table)以及下标的含义确定递推公式dp数组如何初始化确定遍历顺序举例推导dp数组&a…...

Leetcode 206. 反转链表
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。 示例 1: 输入:head [1,2,3,4,5] 输出:[5,4,3,2,1] 示例 2: 输入:head [1,2] 输出:[2,1] 示例 3: 输…...

电子科技大学课程《计算机网络系统》(持续更新)
前言 本校的课程课时有所缩减,因此可能出现与你学习的课程有所减少的情况,因此对其他学校的同学更多的作为参考作用。本文章适合学生的期中期末考试,以及想要考研电子科技大学的同学,电子科技大学同学请先看附言。 第一章 计算…...
HBase介绍、特点、应用场景、生态圈
目录: 一、HBase简介 二、NoSQL和关系型数据库对比 三、HBase特点 四、应用场景 五、HBase生态圈技术 一、HBase简介 HBase是一个领先的NoSQL数据库 是一个面向列存储的NoSQL数据库 是一个分布式Hash Map,底层数据是Key-Value格式 基于Coogle Big Table论文 使用HD…...

深度学习在微纳光子学中的应用
深度学习在微纳光子学中的主要应用方向 深度学习与微纳光子学的结合主要集中在以下几个方向: 逆向设计 通过神经网络快速预测微纳结构的光学响应,替代传统耗时的数值模拟方法。例如设计超表面、光子晶体等结构。 特征提取与优化 从复杂的光学数据中自…...
ES6从入门到精通:前言
ES6简介 ES6(ECMAScript 2015)是JavaScript语言的重大更新,引入了许多新特性,包括语法糖、新数据类型、模块化支持等,显著提升了开发效率和代码可维护性。 核心知识点概览 变量声明 let 和 const 取代 var…...

Opencv中的addweighted函数
一.addweighted函数作用 addweighted()是OpenCV库中用于图像处理的函数,主要功能是将两个输入图像(尺寸和类型相同)按照指定的权重进行加权叠加(图像融合),并添加一个标量值&#x…...

基于当前项目通过npm包形式暴露公共组件
1.package.sjon文件配置 其中xh-flowable就是暴露出去的npm包名 2.创建tpyes文件夹,并新增内容 3.创建package文件夹...

页面渲染流程与性能优化
页面渲染流程与性能优化详解(完整版) 一、现代浏览器渲染流程(详细说明) 1. 构建DOM树 浏览器接收到HTML文档后,会逐步解析并构建DOM(Document Object Model)树。具体过程如下: (…...
【碎碎念】宝可梦 Mesh GO : 基于MESH网络的口袋妖怪 宝可梦GO游戏自组网系统
目录 游戏说明《宝可梦 Mesh GO》 —— 局域宝可梦探索Pokmon GO 类游戏核心理念应用场景Mesh 特性 宝可梦玩法融合设计游戏构想要素1. 地图探索(基于物理空间 广播范围)2. 野生宝可梦生成与广播3. 对战系统4. 道具与通信5. 延伸玩法 安全性设计 技术选…...
大语言模型(LLM)中的KV缓存压缩与动态稀疏注意力机制设计
随着大语言模型(LLM)参数规模的增长,推理阶段的内存占用和计算复杂度成为核心挑战。传统注意力机制的计算复杂度随序列长度呈二次方增长,而KV缓存的内存消耗可能高达数十GB(例如Llama2-7B处理100K token时需50GB内存&a…...

Kafka入门-生产者
生产者 生产者发送流程: 延迟时间为0ms时,也就意味着每当有数据就会直接发送 异步发送API 异步发送和同步发送的不同在于:异步发送不需要等待结果,同步发送必须等待结果才能进行下一步发送。 普通异步发送 首先导入所需的k…...
深度剖析 DeepSeek 开源模型部署与应用:策略、权衡与未来走向
在人工智能技术呈指数级发展的当下,大模型已然成为推动各行业变革的核心驱动力。DeepSeek 开源模型以其卓越的性能和灵活的开源特性,吸引了众多企业与开发者的目光。如何高效且合理地部署与运用 DeepSeek 模型,成为释放其巨大潜力的关键所在&…...
人工智能 - 在Dify、Coze、n8n、FastGPT和RAGFlow之间做出技术选型
在Dify、Coze、n8n、FastGPT和RAGFlow之间做出技术选型。这些平台各有侧重,适用场景差异显著。下面我将从核心功能定位、典型应用场景、真实体验痛点、选型决策关键点进行拆解,并提供具体场景下的推荐方案。 一、核心功能定位速览 平台核心定位技术栈亮…...