网站首页 > java教程 正文
前言
JSON作为一种轻量级的数据交换格式,在我们日常的开发中使用十分广泛,就Java后端的开发工作中,JSON字符串与Java对象之间相互转换是常常遇到的操作。
虽然平时用到的挺多的,但是因为用于JSON处理的包有很多种,每种工具集的功能和使用方式也都不同,很容易在使用时造成混乱。
本文就结合FastJson部分源码,简单整理了下常用的API及其使用示例
本文FastJson版本:1.2.54
转换图
根据FastJson源码大致整理出了这么一张转换图:
可以看到参与转换的对象主要有图中五种:
- JSONString:json字符串
- JSONObject:json对象
- JSONArray:json对象数组
- JavaBean:java对象
- List:java对象集合
转化中用到的方法的方法名有如下几种:
- parse: JSONString ==> JSONObject/JSONArray
- parseObject: JSONString ==> JSONObject/JavaBean
- pareseArray: JSONString ==> JSONObject/List<JavaBean>
- toJSONString: JavaBean/JSONObject ==> JSONString
- toJSON: JavaBean ==> JSONObject
- toJavaObject:JSONObject ==> JavaBean
常用API
本文种仅列举平常使用频率较高的API,其他的重载方法可参考源码,大都是对序列化/反序列化过程进行定制化。
toJSONString#
实现了json对象(JSONObject)>json字符串(JSONString),和Java对象(JavaBean)>json字符串(JSONString)的转化
从源码中可以看到这一方法被重载了多个,我们日常会用到的有如下几个:
方法 : 返回值 | 参数说明 | 功能 |
toJSONString(Object object):String | object: 需要进行序列化的对象javaBean或者JSONObject | 将对象序列化为json字符串 |
toJSONString(Object object, boolean prettyFormat):String | prettyFormat:是否格式化输出json字符串 | 格式化输出json字符串 |
toJSONString(Object object, SerializerFeature... features):String | features:序列化额外属性配置,非必填 | 根据指定属性进行序列化 |
toJSONStringWithDateFormat(Object object, String dateFormat, SerializerFeature... features):String | dateFormat:日期格式(yyyy-MM-dd) | 序列化时格式化日期 |
这些方法中最常用的即为:toJSONString(Object object)
parse#
实现了json字符串(JSONString)>json对象(JSONObject),和json字符串(JSONString)>json对象数组(JSONArray)的转化
方法 : 返回值 | 参数说明 | 功能 |
parse(String text):Object | text:json字符串 | 反序列化json字符串 |
parseObject#
实现了json字符串(JSONString)>json对象(JSONObject),和json字符串(JSONString)>Java对象(JavaBean)的转化
方法 : 返回值 | 参数说明 | 功能 |
parseObject(String text):JSONObject | text:json字符串 | 反序列化json字符串为Json对象 |
parseObject(String text, Class clazz):T | clazz:指定反序列化后的类 | json字符串转java对象 |
parseObject(String text, TypeReference type, Feature... features):T | type:构造转化类型,features:反序列化额外属性 | json字符串转java对象 |
parseArray#
实现了json字符串(JSONString)==>json对象数组(JSONArray),和json字符串(JSONString)==>Java对象集合(List`)的转化
方法 : 返回值 | 参数说明 | 功能 |
parseArray(String text) :JSONArray | text:json字符串 | 将json字符串反序列化为JSON数组对象 |
parseArray(String text, Class clazz):List | clazz:指定转化后的类 | 将json字符串反序列化为java对象集合 |
toJSON/toJavaObject#
toJSON()实现了Java对象(JavaBean)==>Json对象(JSONObject)的转换
toJavaObject()实现了Json对象(JSONObject)==>Java对象(JavaBean)的转换
方法 : 返回值 | 参数说明 | 功能 |
toJSON(Object javaObject):Object | javaObject:java对象 | java对象转化为Json对象 |
toJavaObject(JSON json, Class clazz):T | json:json对象,clazz:要转化的类型 | json对象转化为java对象 |
代码示例
Student学生类
package com.larscheng.www.jsontest;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Date;
/**
* 描述:
* 学生类
*
* @author larscheng
* @date 2019/11/19 19:33
*/
@Data
@AllArgsConstructor
public class Student {
private String name;
private int age;
private Date birthday;
}
测试类FastJsonTest.java代码如下:
package com.larscheng.www.jsontest.fastJson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.larscheng.www.jsontest.Course;
import com.larscheng.www.jsontest.Student;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* 描述:
* fastJson的api示例
*
* @author larscheng
* @date 2019/11/19 19:37
*/
public class FastJsonTest {
private final static Student LIMING = new Student("liming", 20, new Date());
private final static String LIMING_STR =
"{'age':20,'birthday':1574163958480,'name':'liming'}";
private final static Course MATH = new Course("数学课", "高等代数");
private final static Course CHINESE = new Course("语文课", "大学语文");
private final static List<Course> COURSES = Arrays.asList(MATH, CHINESE);
private final static String COURSES_STR =
"[{'desc':'高等代数','name':'数学课'},{'desc':'大学语文','name':'语文课'}]";
private final static JSONObject LIMING_MAP = new JSONObject();
static {
LIMING_MAP.put("name", "liming");
LIMING_MAP.put("age", 20);
LIMING_MAP.put("birthday", new Date());
}
public static void main(String[] args) {
//############ toJSONString ###############
/*JavaBean--->JSONString*/
System.err.println("JavaBean--->JSONString(默认无格式):");
System.out.println(JSON.toJSONString(LIMING));
System.err.println("JavaBean--->JSONString(带格式):");
System.out.println(JSON.toJSONString(LIMING, true));
System.err.println("JavaBean--->JSONString(日期格式化):");
System.out.println(JSON.toJSONStringWithDateFormat(LIMING, "yyyy-MM-dd") + "\n");
/*JSONObject--->JSONString*/
System.err.println("JSONObject--->JSONString(带格式):");
System.out.println(JSON.toJSONString(LIMING_MAP, true) + "\n");
/*List<JavaBean>--->JSONString*/
System.err.println("List<JavaBean>--->JSONString(默认双引号):");
System.out.println(JSON.toJSONString(COURSES));
System.err.println("List<JavaBean>--->JSONString(单引号):");
System.out.println(JSON.toJSONString(COURSES, SerializerFeature.UseSingleQuotes));
System.err.println("List<JavaBean>--->JSONString(单引号+带格式):");
System.out.println(JSON.toJSONString(COURSES, SerializerFeature.UseSingleQuotes,SerializerFeature.PrettyFormat) + "\n");
//########## parse/parseObject ###################
/*JSONString--->JSONObject*/
System.err.println("JSONString--->JSONObject(parse):");
JSONObject jsonObject1 = (JSONObject) JSON.parse(LIMING_STR);
System.out.println(jsonObject1.toString());
System.err.println("JSONString--->JSONObject(parseObject):");
System.out.println(JSON.parseObject(LIMING_STR).toString() + "\n");
System.err.println("JSONString--->JavaBean:");
Student student1 = JSON.parseObject(LIMING_STR,Student.class);
System.out.println(student1.hashCode()+"\t"+student1.toString());
System.err.println("JSONString--->JavaBean:");
Student student2 = JSON.parseObject(LIMING_STR,new TypeReference<Student>(){});
System.out.println(student2.hashCode()+"\t"+student2.toString());
//########### parse/parseArray ################
/*JSONString--->JSONArray*/
System.err.println("JSONString--->JSONArray(parse):");
JSONArray jsonArray1 = (JSONArray) JSON.parse(COURSES_STR);
System.out.println(jsonArray1.toString());
System.err.println("JSONString--->JSONArray(parseArray):");
System.out.println(JSON.parseArray(COURSES_STR).toString());
System.err.println("JSONString--->List<JavaBean>:");
List<Course> courses1 = JSON.parseArray(COURSES_STR,Course.class);
System.out.println(courses1.hashCode()+"\t"+courses1.toString()+"\n");
//######### toJSON/toJavaObject ################
System.err.println("JavaBean--->JSONObject:");
System.out.println(JSON.toJSON(LIMING));
System.err.println("JSONObject--->JavaBean:");
System.out.println(JSON.toJavaObject(LIMING_MAP,Student.class));
System.out.println(LIMING_MAP.toJavaObject(Student.class));
System.out.println((Student)LIMING_MAP.toJavaObject(new TypeReference<Student>(){}));
System.out.println(LIMING_MAP.toJavaObject(new TypeReference<Student>(){}.getType())+"\n");
}
}
总结
基本常用的方法都进行了代码测试,使用过程中可能会出现混淆的情况,但是只要记住了文中的转换图,相信应该会加深印象。
猜你喜欢
- 2024-10-10 简单的对象转换方法类分享(对象转化为字符串的几种方式)
- 2024-10-10 为什么 JSON 不适合作为配置语言?
- 2024-10-10 JSON 对象的这些操作和使用场景你知道多少?
- 2024-10-10 第27天|Java入门有野,json操作(java的json解析几种方法)
- 2024-10-10 如何使用springmvc返回json格式的数据?
- 2024-10-10 springboot从小白到大神-007处理Json数据进阶
- 2024-10-10 Fastjson2如何进行JSON的解析和对象序列化?
- 2024-10-10 Java笔试题目分享(2)知识点总结——Json对象
- 2024-10-10 面试官问,你知道http请求怎么在你的项目中变成Java对象吗?
- 2024-10-10 Spring Boot 中使用FastJSON来替换默认的JSON数据序列方式?
你 发表评论:
欢迎- 最近发表
-
- 你真的会用 Java 中的线程池吗?多个企业级线程池工具类封装实践
- 线程池的实现原理、优点与风险、以及四种线程池实现
- Java线程池ThreadPoolExecutor实现原理剖析
- 深入分析线程池的实现原理(线程池是干嘛的)
- 一文搞懂JAVA线程池工作原理(java线程池的工作流程)
- Java线程池的工作原理(java线程池的实现原理)
- 5分钟读懂C#中TcpClient、TcpListener和Socket三个类的角色
- JVM对象的创建过程(jvm运行过程中创建的对象一般存放在方法区)
- 对象组成与Java内存模型JMM分析(java对象在内存中存储的结构)
- JVM对象内存分配详细过程(栈上分配->TLAB->老年代->Eden区)
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)