fastjson的使用

來源:互聯網
上載者:User

標籤:bean   轉換   原理   out   i++   nbsp   array   泛型   int   

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

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

JSONObject:fastJson提供的json對象。

JSONArray:fastJson提供json數組對象。

我們可以把JSONObject當成一個Map<String,Object>來看,只是JSONObject提供了更為豐富便捷的方法,方便我們對於對象屬性的操作。我們看一下源碼。

同樣我們可以把JSONArray當做一個List<Object>,可以把JSONArray看成JSONObject對象的一個集合。

此外,由於JSONObject和JSONArray繼承了JSON,所以說也可以直接使用兩者對JSON格式字串與JSON對象及javaBean之間做轉換,不過為了避免混淆我們還是使用JSON。

 

首先定義三個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}]}";

 

樣本1:JSON格式字串與JSON對象之間的轉換。

樣本1.1-json字串-簡單對象型與JSONObject之間的轉換

    /**     * 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。

       最後說一點,我們作為程式員,研究問題還是要仔細深入一點的。當你對原理瞭解的有夠透徹,開發起來也就得心應手了,很多開發中的問題和疑惑也就迎刃而解了,而且在面對其他問題的時候也可做到觸類旁通。當然在開發中沒有太多的時間讓你去研究原理,開發中要以實現功能為前提,可等項目上線的後,你有大把的時間或者空餘的時間,你大可去刨根問底,深入的去研究一項技術,為覺得這對一名程式員的成長是很重要的事情。

fastjson的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.