Fastjson虽然最近频爆问题,但是作为Json使用较为广泛的库,与Gson、Jackson一起仍然是众多开发者的重要选择,即使出于安全的角度考虑,从了解fastjson的使用方式以便替换的角度,也需要了解一下fastjson的使用方式,这篇文章进行简单的总结和整理。
maven引用
使用1.2.70版本是官方目前对于2020年度发现的0-day漏洞的对应的解决方案,建议使用此版或者更新的版本。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
三个类
fastjson中主要有三个类:
- JSON
- JSONObject
- JSONArray
说明:JSONObject和JSONArray均继承于JSON类,JSON类本身为一个抽象类,JSONObject用于json对象处理,JSONArray用于json对象数组处理,由于JSON本身为抽象类,所以其无法通过实例化来进行相应操作,主要通过其静态函数进行相关的parse转化操作。
使用示例
整体说明:三个主要的类中,JSON主要通过使用其静态函数进行转换,比如从字符串到Json对象,从Json对象到字符串。JSONObject用于处理Json类,JSONArray用于处理Json数组,就像Json可以组合一样JSONArray和JSONObject也可以同样组成复杂的Json结构。
- JSON定义
public abstract class JSON implements JSONStreamAware, JSONAware {
- JSONObject定义
public class JSONObject extends JSON implements Map<String, Object>, Cloneable, Serializable, InvocationHandler {
- JSONArray定义
public class JSONArray extends JSON implements List<Object>, Cloneable, RandomAccess, Serializable {
示例1: Json对象和字符串
- 示例代码
final String JSON_PERSON_STRING = "{\"name\":\"liumiao\",\"id\":1001}";
final JSONObject json = JSON.parseObject(JSON_PERSON_STRING);
System.out.println("name: " + json.get("name") + " id:" + json.get("id"));
System.out.println(JSON.toJSONString(json));
- 执行结果
name: liumiao id:1001
{"name":"liumiao","id":1001}
示例2: Json数组和字符串
- 示例代码
final String JSON_PERSONS_STRING = "[{\"name\":\"liumiao\",\"id\":1001},{\"name\":\"miaoliu\",\"id\":1002}]";
final JSONArray jsons = JSON.parseArray(JSON_PERSONS_STRING);
System.out.println(JSON.toJSONString(jsons));
System.out.println("loop using size()");
for(int i=0; i<jsons.size(); i++){
JSONObject json = jsons.getJSONObject(i);
System.out.println("name: " + json.get("name") + " id:" + json.get("id"));
}
System.out.println("loop using iterator");
Iterator<Object> iterator = jsons.iterator();
while(iterator.hasNext()) {
JSONObject json = (JSONObject) iterator.next();
System.out.println("name: " + json.get("name") + " id:" + json.get("id"));
}
- 执行结果
[{"name":"liumiao","id":1001},{"name":"miaoliu","id":1002}]
loop using size()
name: liumiao id:1001
name: miaoliu id:1002
loop using iterator
name: liumiao id:1001
name: miaoliu id:1002
示例3: Json组合结构和字符串
- 示例代码
final String JSON_PERSONS_STRING = "[{\"name\":\"liumiao\",\"id\":1001,\"courses\":" +
"[{\"course\":\"math\",\"score\":90},{\"course\":\"english\",\"score\":80}]}," +
"{\"name\":\"miaoliu\",\"id\":1002,,\"courses\":" +
"[{\"course\":\"math\",\"score\":80},{\"course\":\"english\",\"score\":90}]}]";
final JSONArray jsons = JSON.parseArray(JSON_PERSONS_STRING);
System.out.println(JSON.toJSONString(jsons));
System.out.println("loop using size()");
for (int i = 0; i < jsons.size(); i++) {
JSONObject json = jsons.getJSONObject(i);
System.out.println("name: " + json.get("name") + " id:" + json.get("id"));
JSONArray courses = json.getJSONArray("courses");
for(int j=0; j<courses.size(); j++) {
JSONObject course = courses.getJSONObject(j);
System.out.println("course: " + course.get("course") + " score:" + course.get("score"));
}
}
System.out.println("loop using iterator");
Iterator<Object> iterator = jsons.iterator();
while (iterator.hasNext()) {
JSONObject json = (JSONObject) iterator.next();
System.out.println("name: " + json.get("name") + " id:" + json.get("id"));
JSONArray courses = json.getJSONArray("courses");
Iterator<Object> courseIterator = courses.iterator();
while(courseIterator.hasNext()){
JSONObject course = (JSONObject) courseIterator.next();
System.out.println("course: " + course.get("course") + " score:" + course.get("score"));
}
}
- 执行结果
[{"courses":[{"score":90,"course":"math"},{"score":80,"course":"english"}],"name":"liumiao","id":1001},{"courses":[{"score":80,"course":"math"},{"score":90,"course":"english"}],"name":"miaoliu","id":1002}]
loop using size()
name: liumiao id:1001
course: math score:90
course: english score:80
name: miaoliu id:1002
course: math score:80
course: english score:90
loop using iterator
name: liumiao id:1001
course: math score:90
course: english score:80
name: miaoliu id:1002
course: math score:80
course: english score:90
示例4: Json对象和JavaBean
- 示例代码
public class Person {
private int id;
private String 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 Person(int id, String name) {
this.id = id;
this.name= name;
}
}
转化示例代码
final String JSON_PERSON_STRING = "{\"name\":\"liumiao\",\"id\":1001}";
final JSONObject json = JSON.parseObject(JSON_PERSON_STRING);
String name = json.getString("name");
System.out.println("name: " + json.get("name") + " id:" + json.get("id"));
// using new
Person person1 = new Person(json.getInteger("id"),json.getString("name"));
System.out.println(JSON.toJSONString(person1));
// using JSON.parsObject
Person person2 = JSON.parseObject(JSON_PERSON_STRING, Person.class);
System.out.println(JSON.toJSONString(person2));
// using JSON.parsObject
Person person3 = JSON.parseObject(JSON_PERSON_STRING, new TypeReference<Person>() {});
System.out.println(JSON.toJSONString(person3));
- 执行结果
name: liumiao id:1001
{"id":1001,"name":"liumiao"}
{"id":1001,"name":"liumiao"}
{"id":1001,"name":"liumiao"}
示例5: Json数组和JavaBean
- 示例代码
final String JSON_PERSONS_STRING = "[{\"name\":\"liumiao\",\"id\":1001},{\"name\":\"miaoliu\",\"id\":1002}]";
final JSONArray jsons = JSON.parseArray(JSON_PERSONS_STRING);
System.out.println(JSON.toJSONString(jsons));
List<Person> personList1 = new ArrayList<Person>();
Iterator<Object> iterator = jsons.iterator();
while(iterator.hasNext()) {
JSONObject json = (JSONObject) iterator.next();
personList1.add(new Person(json.getInteger("id"),json.getString("name")));
}
System.out.println(JSON.toJSONString(personList1));
List<Person> personList2 = JSON.parseArray(JSON_PERSONS_STRING,Person.class);
System.out.println(JSON.toJSONString(personList2));
List<Person> personList3 = JSON.parseObject(JSON_PERSONS_STRING, new TypeReference<ArrayList<Person>>() {});
System.out.println(JSON.toJSONString(personList3));
List<Person> personList4 = new ArrayList<Person>();
personList4.add(new Person(2001,"liu"));
personList4.add(new Person(2002,"miao"));
personList4.add(new Person(2003,"liumiao"));
System.out.println(JSON.toJSONString(personList4));
- 执行结果
[{"name":"liumiao","id":1001},{"name":"miaoliu","id":1002}]
[{"id":1001,"name":"liumiao"},{"id":1002,"name":"miaoliu"}]
[{"id":1001,"name":"liumiao"},{"id":1002,"name":"miaoliu"}]
[{"id":1001,"name":"liumiao"},{"id":1002,"name":"miaoliu"}]
[{"id":2001,"name":"liu"},{"id":2002,"name":"miao"},{"id":2003,"name":"liumiao"}]
示例6: Json组合结构和JavaBean
- 示例代码
public class Person {
private int id;
private String name;
private List<Course> courses;
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
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 Person(int id, String name, List<Course> courses) {
this.id = id;
this.name= name;
this.courses=courses;
}
}
public class Course {
private String course;
private int score;
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
final String JSON_PERSON_STRING = "{\"name\":\"liumiao\",\"id\":1001,\"courses\":" +
"[{\"course\":\"math\",\"score\":90},{\"course\":\"english\",\"score\":80}]}";
final JSONObject json = JSON.parseObject(JSON_PERSON_STRING);
System.out.println(JSON.toJSONString(json));
Person person1 = JSON.parseObject(JSON_PERSON_STRING, Person.class);
System.out.println(JSON.toJSONString(person1));
Person person2 = JSON.parseObject(JSON_PERSON_STRING,new TypeReference<Person>() {});
System.out.println(JSON.toJSONString(person2));
- 执行结果
{"courses":[{"score":90,"course":"math"},{"score":80,"course":"english"}],"name":"liumiao","id":1001}
{"courses":[{"course":"math","score":90},{"course":"english","score":80}],"id":1001,"name":"liumiao"}
{"courses":[{"course":"math","score":90},{"course":"english","score":80}],"id":1001,"name":"liumiao"}
参考内容
https://github.com/alibaba/fastjson
https://github.com/alibaba/fastjson/wiki/Quick-Start-CN
https://mvnrepository.com/artifact/com.alibaba/fastjson/