1.fastjson.jar包的导入
贴上fastjson在GitHub上的地址fastjson
2.同时下载fastjson的源码
将源码导入IDE中
3.开始测试
3.1抽象类JSON的介绍
JSON类是使用fastjson的主要类,通常调用
该类的两个静态方法toJSONString(Object)和parseObject(String,Class)。toJSONString(Object)方法实现将Java对象转换为JSON字符串,parseObject(String,Class)方法将字符串解析为Class指定的Java对象,如果缺省Class参数,则把String解析为JSONObject类型的对象。
public class fastJsonTest {
public static void main(String[] args) {
Student student = new Student("王五",28,"10003");
Object o = JSON.toJSON(student);
System.out.println(o.getClass());
String s = JSON.toJSONString(student);
System.out.println(s);
System.out.println("=====================");
String stu = "{\"age\":28,\"classNum\":\"10003\",\"name\":\"王五\"}";
Object stuO = JSON.parseObject(stu,Student.class);
System.out.println(stuO.getClass());
Object o1 = JSON.parse(stu);
System.out.println(o1.getClass());
}
运行结果如下:
class com.alibaba.fastjson.JSONObject
{"age":28,"classNum":"10003","name":"王五"}
=====================
class lgp.learn.string010301.Student
class com.alibaba.fastjson.JSONObject
3.2 ParameterizedType
parameterizedType 是位于java.lang.reflect包中的接口,Type接口的子接口,API中是这样解释的“ParameterizedType 表示参数化类型,例如 Collection<String>”。
如果你正在序列化/反序列化的对象是ParameterizedType类型的,即参数化类型的对象,需要使用JSON类的toJSONStirng(Object)方法和parseObject(String,Type,Feature[])方法,测试代码如下:
String s = "[{\"age\":23,\"classNum\":\"10001\",\"name\":\"张三\"}," +
"{\"age\":24,\"classNum\":\"10002\",\"name\":\"李四\"}," +
"{\"age\":25,\"classNum\":\"10003\",\"name\":\"王五\"}]";
System.out.println("将字符串s解析为Student类型的列表方法1:");
Type listType = new TypeReference<List<Student>>(){}.getType();
List<Student> list = JSON.parseObject(s,listType);
for(Student stu:list) System.out.println(stu);
System.out.println("==================");
System.out.println("将字符串s解析为Student类型的列表方法2:");
JSONArray ja = JSONArray.parseArray(s);
for(Object o:ja){
JSONObject jo = (JSONObject) o;
System.out.print(jo.getClass());
System.out.print(":"+jo);
System.out.println();
Student stu = JSON.parseObject(o.toString(),Student.class);
System.out.print(stu.getClass());
System.out.print(":"+stu);
System.out.println();
}
System.out.println("==================");
System.out.println("将Student类型的列表解析为字符串:");
ArrayList<Student> stus = new ArrayList<>();
stus.add(new Student("张三",23,"10001"));
stus.add(new Student("李四",24,"10002"));
stus.add(new Student("王五",25,"10003"));
String students = JSON.toJSONString(stus);
System.out.println(students);
运行结果
将字符串s解析为Student类型的列表方法1:
Student{name='张三', age=23, classNum='10001'}
Student{name='李四', age=24, classNum='10002'}
Student{name='王五', age=25, classNum='10003'}
==================
将字符串s解析为Student类型的列表方法2:
class com.alibaba.fastjson.JSONObject:{"name":"张三","classNum":"10001","age":23}
class lgp.learn.string010301.Student:Student{name='张三', age=23, classNum='10001'}
class com.alibaba.fastjson.JSONObject:{"name":"李四","classNum":"10002","age":24}
class lgp.learn.string010301.Student:Student{name='李四', age=24, classNum='10002'}
class com.alibaba.fastjson.JSONObject:{"name":"王五","classNum":"10003","age":25}
class lgp.learn.string010301.Student:Student{name='王五', age=25, classNum='10003'}
==================
将Student类型的列表解析为字符串:
[{"age":23,"classNum":"10001","name":"张三"},{"age":24,"classNum":"10002","name":"李四"},{"age":25,"classNum":"10003","name":"王五"}]