1、遵循http://json.org标准,为其官方网站收录的参考实现之一。
2、功能qiang打,支持JDK的各种类型,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。
3、无依赖,不需要例外额外的jar,能够直接跑在JDK上。
4、开源,使用Apache License 2.0协议开源。http://code.alibabatech.com/wiki/display/FastJSON/Home
SVN:http://code.alibabatech.com/svn/fastjson/trunk/
WIKI:http://code.alibabatech.com/wiki/display/FastJSON/Home
Issue Tracking:http://code.alibabatech.com/jira/browse/FASTJSON
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
Student.java 和Teacher.java 的代码如下:
package com.android.fastjson.bean; public class Student { private int id;
private String name;
private int age; /**
* 默认的构造方法必须不能省,不然不能解析
*/ public Student(){ }
public Student(int id,String name,int age) {
this.id = id;
this.name = name;
this.age = age;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
} }
package com.android.fastjson.bean; public class Student { private int id;
private String name;
private int age; /**
* 默认的构造方法必须不能省,不然不能解析
*/ public Student(){ }
public Student(int id,String name,int age) {
this.id = id;
this.name = name;
this.age = age;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
} }
package com.android.fastjson.bean; import java.util.List; public class Teacher { private int id;
private String name; private List<Student> students; /**
* 默认的构造方法必须不能省,不然不能解析
*/
public Teacher() { }
public Teacher(int id,String name) {
this.id = id;
this.name = name;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + ", mStudents="
+ students + "]";
} }
package com.android.fastjson.bean; import java.util.List; public class Teacher { private int id;
private String name; private List<Student> students; /**
* 默认的构造方法必须不能省,不然不能解析
*/
public Teacher() { }
public Teacher(int id,String name) {
this.id = id;
this.name = name;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + ", mStudents="
+ students + "]";
} }
上面 的代码比较简单,主要就几个属性,这里值得注意的是, 默认的构造函数一定要写,不然是无法解析的。(对于fastjson 严格按照JavaBean的规范来,有一点不对就无法解析,这里一定要切记,每一个实体类的属性的get , set 方法必须写,切方法第四个字母必须大写,最好使用Eclipse的source->genreal setters and getters 生成get,set 方法,切记,切记 )。对于Gson 这里就没有这么多的要求了,但最好还是按照JavaBean来写,避免一些位置的错误。
上面主要基本的介绍完了,就开始解析,首先要下载fastjson 和Gson的jar包,这个网上很多请自行下载。
首先看 fastjson 的解析
首先解析一个实体类为json 格式。
Student student = new Student(, "Aaron", );
System.out.println(JSON.toJSONString(student));
Student student = new Student(, "Aaron", );
System.out.println(JSON.toJSONString(student));
输出结果为:{"age":,"id":,"name":"Aaron"}
输出结果为:{"age":,"id":,"name":"Aaron"}
输出的json格式与我们的Student中定义的属性其实是不一致的,这个其实不影响我们生成以及后面的解析,因为他有严格的get和set方法定义。
更复杂一点的数据类型也可以很方便的使用JSON.toJsonString(Object o) 这个方法进行解析成json格式。
List<Student> students = new ArrayList<Student>();
for(int i=;i<;i++) {
Student stu = new Student(i, "Student" + i, +i);
students.add(stu);
}
List<Student> students = new ArrayList<Student>();
for(int i=;i<;i++) {
Student stu = new Student(i, "Student" + i, +i);
students.add(stu);
}
解析结果为:
[{"age":,"id":,"name":"Student0"},{"age":,"id":,"name":"Student1"},{"age":,"id":,"name":"Student2"},{"age":,"id":,"name":"Student3"},{"age":,"id":,"name":"Student4"}]
[{"age":,"id":,"name":"Student0"},{"age":,"id":,"name":"Student1"},{"age":,"id":,"name":"Student2"},{"age":,"id":,"name":"Student3"},{"age":,"id":,"name":"Student4"}]
我们来看看更复杂一点的结构。 我们将一个Teacher 列表来解析,一个Teacher对象中是包含Student 列表的这个也在我们常用的数据模型,下面看看代码:
List<Teacher> teaList = new ArrayList<Teacher>();
long time = System.currentTimeMillis();
for(int i=;i<;i++) {
Teacher teacher = new Teacher(i, "Teacher " + i);
List<Student> stus = new ArrayList<Student>();
for(int j = ;j<;j++) {
Student s = new Student(j, "Student" + j, +j);
stus.add(s);
}
teacher.setStudents(stus);
teaList.add(teacher);
}
String jsonTeach = JSON.toJSONString(teaList);
System.out.println("fastjson = " + jsonTeach);
List<Teacher> teaList = new ArrayList<Teacher>();
long time = System.currentTimeMillis();
for(int i=;i<;i++) {
Teacher teacher = new Teacher(i, "Teacher " + i);
List<Student> stus = new ArrayList<Student>();
for(int j = ;j<;j++) {
Student s = new Student(j, "Student" + j, +j);
stus.add(s);
}
teacher.setStudents(stus);
teaList.add(teacher);
}
String jsonTeach = JSON.toJSONString(teaList);
System.out.println("fastjson = " + jsonTeach);
结果为:
fastjson = [{"id":,"name":"Teacher 0","students":[{"age":,"id":,"name":"Student0"},{"age":,"id":,"name":"Student1"}]},{"id":,"name":"Teacher 1","students":[{"age":,"id":,"name":"Student0"},{"age":,"id":,"name":"Student1"}]},{"id":,"name":"Teacher 2","students":[{"age":,"id":,"name":"Student0"},{"age":,"id":,"name":"Student1"}]},{"id":,"name":"Teacher 3","students":[{"age":,"id":,"name":"Student0"},{"age":,"id":,"name":"Student1"}]}]
还是一个简单的函数而已,就能办到。
这个还有toJSONString 重载的方法,有很多,在给一个就是可以按照标准的json 格式输出,上面的输出都不规范,我们来看看
Student student = new Student(, "Aaron", );
System.out.println(JSON.toJSONString(student,true));
输出为:
{
"age":,
"id":,
"name":"Aaron"
}
如果我们想要将实体类中的某个字段或某几个不进行解析呢?那么我们可以使用transient 关键字,来标记它为不需要的,在fastjson中还提供了一种便捷的方法来自定义我们需要序列化的字段
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(实体类.class, "字段1","字段2"); 字段为我们需要序列化的字段,如果实体类中没有改字段则不解析放弃该字段而不会报错。
如下:
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Student.class, "id","age");
String jsonStu =JSON.toJSONString(students,filter);
这样就只会序列化 id和age 的字段。
下面进行fastjson 的反序列化,这个也是很常用的功能,
还是拿最简单的来进行示范
Student student = new Student(, "Aaron", );
String str = JSON.toJSONString(student,true);
System.out.println(JSON.parseObject(str,Student.class));
反序列化主要使用的是JSON.parseObject(str,class); 它就能把我们的str 转化为class 类型的对象,当然还有更复杂的对象,比如:
List<Student> students = new ArrayList<Student>();
for(int i=;i<;i++) {
Student stu = new Student(i, "Student" + i, +i);
students.add(stu);
}
// 过滤哪些属性需要转换
// SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Student.class, "id","age");
// String jsonStu =JSON.toJSONString(students,filter);
String jsonStu =JSON.toJSONString(students);
System.out.println(jsonStu); List<Student> stu =JSON.parseObject(jsonStu, new TypeReference<List<Student>>(){});
for(int i=;i<stu.size();i++)
{
System.out.println(stu.get(i));
}
前面就是添加数据,然后在后面解析为json格式,然后在转化为类的对象,这里转为List<Student> 对象,主要用到new TypeReference<数据返回类型> (){},我们使用的就是new TypeReference<List<Student>>(){} 返回的结果就是List<Student>
也不需要强制转化,我们看看上面输出的结果为:
Student [id=, name=Student0, age=]
Student [id=, name=Student1, age=]
Student [id=, name=Student2, age=]
Student [id=, name=Student3, age=]
Student [id=, name=Student4, age=]
当然fastjson 能处理的数据类型还有很多很多,我主要列了我常用的数据类型,今天主要就讲到这里。