【前言】
【Fastjson <= 1.2.68
存在安全漏洞,请使用更高版本】
【总结篇】
json字符串 -> object
// json字符串转换为JSON对象
JSONObject parseObject(String jsonString)
JSONArray parseArray(String jsonString)
// json字符串转换为javaBean
T parseObject(String jsonString, Class<T> clazz)
List<T> parseArray(String jsonString, Class<T> clazz)
object -> json字符串
String toJSONString(Object object)
如果对FastJson比较熟悉的话直接使用总结的API即可(可以略过下面内容),如果不熟悉下面提供具体的步骤和案例
【使用篇】
【maven依赖】
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
【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字符串】
JSON格式字符串 --> JSON对象
json字符串(简单对象型) --> JSONObject
public static void testJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
json字符串(数组类型) --> JSONArray
public static void testJSONStrToJSONArray(){
JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
}
复杂json格式字符串 --> JSONObject
public static void testComplexJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
String teacherName = jsonObject.getString("teacherName");
Integer teacherAge = jsonObject.getInteger("teacherAge");
JSONObject course = jsonObject.getJSONObject("course");
JSONArray students = jsonObject.getJSONArray("students");
}
JSON格式字符串 --> javaBean
准备实体类:
@Data
public class Student {
private String studentName;
private Integer studentAge;
}
@Data
public class Course {
private String courseName;
private Integer code;
}
@Data
public class Teacher {
private String teacherName;
private Integer teacherAge;
private Course course;
private List<Student> students;
}
json字符串(简单对象型) --> javaBean
public static void testJSONStrToJavaBeanObj(){
Student student = JSON.parseObject(JSON_OBJ_STR, Student.class);
System.out.println(student.getStudentName()+":"+student.getStudentAge());
}
json字符串(数组类型) --> javaBean
public static void testJSONStrToJavaBeanList(){
List<Student> students = JSON.parseArray(JSON_ARRAY_STR, Student.class);
for (Student student : students) {
System.out.println(student.getStudentName()+":"+student.getStudentAge());
}
}
复杂json格式字符串 --> javaBean
public static void testComplexJSONStrToJavaBean(){
Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, Teacher.class);
String teacherName = teacher.getTeacherName();
Integer teacherAge = teacher.getTeacherAge();
Course course = teacher.getCourse();
List<Student> students = teacher.getStudents();
}
Object -> JSON字符串
String toJSONString(Object object)