1.简介
FastJSON是一个Java语言编写的标准的JSON库,可以将Java对象转换为JSON格式,也可以将JSON字符串转换为Java对象。
2.常用方法
(1).JavaBean类
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//get和set方法
}
(2).JavaBean转JSONString
public class JsonDemo {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student steven = new Student("steven", 30);
Student owen = new Student("owen", 28);
list.add(steven);
list.add(owen);
System.out.println("*******JavaBean to JSONString*******");
System.out.println("*******Object*******");
String objectStr = JSON.toJSONString(steven);
System.out.println(objectStr);
System.out.println("*******List*******");
String listStr = JSON.toJSONString(list);
System.out.println(listStr);
}
}
*******JavaBean to JSONString*******
*******Object*******
{"age":30,"name":"steven"}
*******List*******
[{"age":30,"name":"steven"},{"age":28,"name":"owen"}]
(3).JSONString转JavaBean
public class JsonDemo {
public static void main(String[] args) {
String objectStr = "{\"age\":30,\"name\":\"steven\"}";
String listStr = "[{\"age\":30,\"name\":\"steven\"},{\"age\":28,\"name\":\"owen\"}]";
System.out.println("******JSONString to JavaBean*******");
System.out.println("*******Object*******");
Student student = JSON.parseObject(objectStr, Student.class);
System.out.println(student);
List<Student> studentList = JSON.parseObject(listStr,new TypeReference<List<Student>>(){});
System.out.println(studentList);
System.out.println();
}
}
******JSONString to JavaBean*******
*******Object*******
Student{name='steven', age=30}
[Student{name='steven', age=30}, Student{name='owen', age=28}]
(4).JavaBean转JSONObject
public class JsonDemo {
public static void main(String[] args) {
System.out.println("******JavaBean to JSONObject******");
Student steven = new Student("steven", 30);
JSONObject stevenJsonObject = (JSONObject) JSON.toJSON(steven);
System.out.println(stevenJsonObject.getString("name"));
System.out.println();
}
}
******JavaBean to JSONObject******
steven
(5).JSONObject to JavaBean
public class JsonDemo {
public static void main(String[] args) {
System.out.println("******JSONObject to JavaBean******");
Student student = new Student("steven", 30);
JSONObject stevenJsonObject= (JSONObject) JSON.toJSON(student);
Student steven = JSON.toJavaObject(stevenJsonObject, Student.class);
System.out.println(steven);
System.out.println();
}
}
******JSONObject to JavaBean******
Student{name='steven', age=30}
(6).JavaBean转JSONArray
public class JsonDemo {
public static void main(String[] args) {
System.out.println("*******JavaBean to JSONArray******");
List<Student> studentList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
studentList.add(new Student("student" + i, i));
}
JSONArray jsonArrays = (JSONArray) JSON.toJSON(studentList);
for (int i = 0; i < jsonArrays.size(); i++) {
System.out.println(jsonArrays.getJSONObject(i));
}
System.out.println();
}
}
*******JavaBean to JSONArray******
{"name":"student0","age":0}
{"name":"student1","age":1}
{"name":"student2","age":2}
{"name":"student3","age":3}
{"name":"student4","age":4}