fastJson

fastJson對於json格式字符串的解析主要用到了一下三個類:html

JSON:fastJson的解析器,用於JSON格式字符串與JSON對象及javaBean之間的轉換。java

JSONObject:fastJson提供的json對象。json

JSONArray:fastJson提供json數組對象。數組

咱們能夠把JSONObject當成一個Map<String,Object>來看,只是JSONObject提供了更爲豐富便捷的方法,方便咱們對於對象屬性的操做。咱們看一下源碼。this

一樣咱們能夠把JSONArray當作一個List<Object>,能夠把JSONArray當作JSONObject對象的一個集合。spa

此外,因爲JSONObject和JSONArray繼承了JSON,因此說也能夠直接使用二者對JSON格式字符串與JSON對象及javaBean之間作轉換,不過爲了不混淆咱們仍是使用JSON。code

 

首先定義三個json格式的字符串,做爲咱們的數據源。htm

複製代碼
//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}]}";
複製代碼

 

示例1:JSON格式字符串與JSON對象之間的轉換。對象

示例1.1-json字符串-簡單對象型與JSONObject之間的轉換blog

複製代碼
    /**
     * json字符串-簡單對象型與JSONObject之間的轉換
     */
    public static void testJSONStrToJSONObject(){

        JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
        //JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //由於JSONObject繼承了JSON,因此這樣也是能夠的

        System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));

    }
複製代碼

 

示例1.2-json字符串-數組類型與JSONArray之間的轉換

複製代碼
    /**
     * json字符串-數組類型與JSONArray之間的轉換
     */
    public static void testJSONStrToJSONArray(){

        JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
        //JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//由於JSONArray繼承了JSON,因此這樣也是能夠的

        //遍歷方式1
        int size = jsonArray.size();
        for (int i = 0; i < size; i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
        }

        //遍歷方式2
        for (Object obj : jsonArray) {
            JSONObject jsonObject = (JSONObject) obj;
            System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
        }
    }
複製代碼

 

示例1.3-複雜json格式字符串與JSONObject之間的轉換

複製代碼
    /**
     * 複雜json格式字符串與JSONObject之間的轉換
     */
    public static void testComplexJSONStrToJSONObject(){

        JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
        //JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//由於JSONObject繼承了JSON,因此這樣也是能夠的
        
        String teacherName = jsonObject.getString("teacherName");
        Integer teacherAge = jsonObject.getInteger("teacherAge");
        JSONObject course = jsonObject.getJSONObject("course");
        JSONArray students = jsonObject.getJSONArray("students");

    }
複製代碼

 

示例2:JSON格式字符串與javaBean之間的轉換。

首先,咱們針對數據源所示的字符串,提供三個javaBean。

複製代碼
public class Student {

    private String studentName;
    private Integer studentAge;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Integer getStudentAge() {
        return studentAge;
    }

    public void setStudentAge(Integer studentAge) {
        this.studentAge = studentAge;
    }
}
複製代碼
複製代碼
public class Course {

    private String courseName;
    private Integer code;

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}
複製代碼
複製代碼
public class Teacher {

    private String teacherName;
    private Integer teacherAge;
    private Course course;
    private List<Student> students;

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public Integer getTeacherAge() {
        return teacherAge;
    }

    public void setTeacherAge(Integer teacherAge) {
        this.teacherAge = teacherAge;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }
}
複製代碼

json字符串與javaBean之間的轉換推薦使用 TypeReference<T> 這個類,使用泛型能夠更加清晰,固然也有其它的轉換方式,這裏就不作探討了。

示例2.1-json字符串-簡單對象型與javaBean之間的轉換

複製代碼
   /**
     * json字符串-簡單對象與JavaBean_obj之間的轉換
     */
    public static void testJSONStrToJavaBeanObj(){

        Student student = JSON.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});
        //Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});//由於JSONObject繼承了JSON,因此這樣也是能夠的

        System.out.println(student.getStudentName()+":"+student.getStudentAge());

    }
複製代碼

示例2.2-json字符串-數組類型與javaBean之間的轉換

複製代碼
/**
     * json字符串-數組類型與JavaBean_List之間的轉換
     */
    public static void testJSONStrToJavaBeanList(){
        
        ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});
        //ArrayList<Student> students1 = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});//由於JSONArray繼承了JSON,因此這樣也是能夠的
        
        for (Student student : students) {
            System.out.println(student.getStudentName()+":"+student.getStudentAge());
        }
    }
複製代碼

示例2.3-複雜json格式字符串與與javaBean之間的轉換

複製代碼
    /**
     * 複雜json格式字符串與JavaBean_obj之間的轉換
     */
    public static void testComplexJSONStrToJavaBean(){

        Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});
        //Teacher teacher1 = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});//由於JSONObject繼承了JSON,因此這樣也是能夠的
        String teacherName = teacher.getTeacherName();
        Integer teacherAge = teacher.getTeacherAge();
        Course course = teacher.getCourse();
        List<Student> students = teacher.getStudents();
    }
複製代碼

對於TypeReference<T>,因爲其構造方法使用 protected 進行修飾,因此在其餘包下建立其對象的時候,要用其實現類的子類:new TypeReference<Teacher>() {}

此外的:

1,對於JSON對象與JSON格式字符串的轉換能夠直接用 toJSONString()這個方法。

2,javaBean與JSON格式字符串之間的轉換要用到:JSON.toJSONString(obj);

3,javaBean與json對象間的轉換使用:JSON.toJSON(obj),而後使用強制類型轉換,JSONObject或者JSONArray。

原文轉https://www.cnblogs.com/cdf-opensource-007/p/7106018.html

相關文章
相關標籤/搜索