首先,在之前的博客我们谈到了google开发的GSON,用来做java Bean对象与JSON之间的转换,来完成前后端数据交互的统一,然后在之后又了解到有一个国产的比GSON效率更高的阿里巴巴开发的fastjson,具体性能对比可参考该作者写的三种技术对比的博客:
Gson、FastJson、org.JSON到底哪一个效率更高,速度更快
https://blog.csdn.net/zml_2015/article/details/52165317
看完这篇博客,驱动着我来一探fastjon究竟.
FastJson的介绍:
JSON协议使用方便,越来越流行,JSON的处理器有很多,这里我介绍一下FastJson,FastJson是阿里的开源框架,被不少企业使用,是一个极其优秀的Json框架,Github地址: FastJson
FastJson的特点:
1.FastJson数度快,无论序列化和反序列化,都是当之无愧的fast
2.功能强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)
3.零依赖(没有依赖其它任何类库)
FastJson的简单说明:
FastJson对于json格式字符串的解析主要用到了下面三个类:
1.JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换
2.JSONObject:fastJson提供的json对象
3.JSONArray:fastJson提供json数组对象
不过上面都是扯淡,我们还是直接来看怎么来使用这么一个强大的工具吧!!!!
首先,我们在maven的配置文件pom.xml中导入它的依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.23</version> </dependency>
ok,我们就可以在项目中使用它了
具体的使用方法:
序列化
序列化就是指 把JavaBean对象转成JSON格式的字符串
String objJson = JSON.toJSONString(Object object); //传入一个对象,将对象转成JSON字符串
反序列化
反序列化就是把JSON格式的字符串转化为Java Bean对象。
1 User user1 = JSON.parseObject(userJson, User.class); 2 System.out.println(user1.getUserName()); //输出结果李四,User是类名
//JSONObject,JSONArray是JSON的两个子类。
//JSONObject相当于Map<String, Object>,
//JSONArray相当于List<Object>。
实例演示:
首先定义三个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}]}";
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);
}
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);
}
简单javaBean与json对象之间的转换
/**
* 简单JavaBean_obj到json对象的转换
*/
@Test
public void testJavaBeanToJSONObject(){
//已知简单JavaBean_obj
Student 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);
}