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

  •  java
周旭 2017年09月17日发布

  • 赞  |   4收藏  |  4
  • 4k 次浏览

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);
}

3.源码

本篇博客的源码都在我的Github上,FastJsonDemo,欢迎大家Fork and Star!

4.总结

好啦,FastJson的基本用法就介绍完了,送人玫瑰,手留余香,学习使我快乐,分享让大家快乐,欢迎大家点赞收藏噢!

  • 2017年09月17日发布

新浪微博

  • 微信
  • Twitter
  • Facebook

高性能JSON框架之FastJson的简单使用相关推荐

  1. NET流行高性能JSON框架-Json.NET

    在日常编程中经常会使用到Json来进行数据的交互好在.Net平台下有很多开源的Json库使得我们能够比较轻松快速的处理各种复杂的Json,其中Newtonsoft库 是NET的流行高性能JSON框架 ...

  2. JSON框架之阿里fastjson的介绍

    前言 1.3 FastJson介绍 在日常的java项目开发中,JSON的使用越来越频繁,对于Json的处理工具也有很多.接下来就介绍一下阿里开源的一个高性能的JSON框架FastJson,功能完善, ...

  3. fastjson list转json字符串_程序员:JSON、JSONObject 与 JSONArray 简单讲解

    fastjson简介 fastjson 是阿里巴巴的开源JSON解析库,它可以解析 JSON 格式的字符串,支持将 Java Bean 序列化为 JSON 字符串,也可以从 JSON 字符串反序列化到 ...

  4. 深入 Go 中各个高性能 JSON 解析库

    深入 Go 中各个高性能 JSON 解析库 转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com/archives/535 其实本来我是没打算 ...

  5. 高性能RPC框架gRPC竟恐怖如斯~

    RPC.gRPC.Thrift.HTTP,大家知道它们之间的联系和区别么?这些都是面试常考的问题,今天带大家先搞懂 RPC 和 gRPC. 在讲述 gRPC 之前,我们需要先搞懂什么是 RPC. 不 ...

  6. Java 常见JSON框架介绍

    JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.其简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言,同时也易于机器解析和生成, ...

  7. TARS-PHP:PHP构建高性能RPC框架

    内容来源:2018 年 5 月 19 日,阅文集团高级开发工程师梁晨在"PHPCon China 2018 技术峰会"进行<TARS-PHP:PHP构建高性能RPC框架> ...

  8. java json开发包 fastjson 简介

    Json是一种轻量级的数据交换格式,采用一种"键:值"对的文本格式来存储和表示数据,在系统交换数据过程中常常被使用,是一种理想的数据交换语言.在使用Java做Web开发时,不可避免 ...

  9. 常用json框架介绍和Jackson返回结果处理

    常用json框架介绍和Jackson返回结果处理     简介:介绍常用json框架和注解的使用,自定义返回json结构和格式 1.常用框架 阿里 fastjson,谷歌gson等         J ...

  10. Google高性能RPC框架gRPC 1.0.0发布

    鉴于gRPC已进入稳定版分支,并对应用于生产中准备就绪,Google发布了gRPC 1.0. gRPC源于被称为Stubby的Google内部项目,早期是用于一些Google内部服务间的通信.18个月 ...

最新文章

  1. [翻译] NumSharp的数组切片功能 [:]
  2. HDU 6755 Fibonacci Sum(二次剩余 + 二项式展开)
  3. 关于非阻塞的recv的时候返回的处理
  4. java需求设计_JavaWeb期末设计---需求分析文档. stage1
  5. WordPress插件、Erphp loggedin汉化版插件、 异地IP登录自动禁封用户
  6. 容器入门(5)- 在Registry之间复制镜像
  7. 谈谈VIP漂移那点破事
  8. 前端学习笔记-JS数据类型
  9. 随机效应估算与固定效应估算_短面板数据学习手册(固定效应与随机效应等汇总)...
  10. 类和对象12:容器方法
  11. 区块链公司BitFury与联合国合作开展哈萨克斯坦的森林项目
  12. 离散复习资料之一(Warshall算法)
  13. 解析USGS网站页面中的地震空间数据
  14. python debugger extension_Pykd——Python extension for WinDbg
  15. legacy引导gpt分区_UEFI+GPT、Legacy+MBR引导模式介绍 引导修复
  16. gazebo添加模型仿真
  17. 【JS】数据结构之树结构
  18. 如何从 Trados双语文件 中获得 原文 的方法
  19. php 排队叫号源码,排队叫号机源代码
  20. 【树莓派】了解wiringPi库、控制继电器

热门文章

  1. SOX命令:音频位深度、采样率以及码率
  2. 再谈微软复兴,纳德拉与库克、马斯克、皮查伊在管理上有什么不同
  3. sklearn笔记18决策树gini系数
  4. ucenter php7.0版,UCenter1.5.0UCenter Home1.5Discuz! 7.0 集成安装包
  5. SqlServer 多服务器管理(MSSQL分布式作业管理)
  6. nodejs+vue结合七牛云实现图片上传
  7. 单机魔域mysql_魔域单机版3.2
  8. 进了小公司的应届程序员如何翻身进入大公司?
  9. 运动神经元病是怎么回事?
  10. 练习 颈部肌肉重要性及锻炼方法