博客地址 : http://blog.csdn.NET/shulianghan/article/details/41011605java
fastjson 源碼地址 : git
-- GitHub : https://github.com/alibaba/fastjson ;github
-- 示例源碼 : https://github.com/alibaba/fastjson/wiki/Samples-DataBind ;json
-- Jar下載 : http://sourceforge.net/projects/fastjson/ ;數組
總結 : ide
Java Bean <--> json 字符串 <--> JSONObject 互相轉化函數
json 字符串 與 java Bean 對象 互相轉化 : this
-- json 字符串 -> Java Bean 對象 : JSON.parseObject(String text, Class<Student> clazz) 方法, 示例 - Student student = JSON.parseObject(json_student, Student.class);spa
-- Java Bean 對象 -> json 字符串 : JSON.toJSONString(Object object, boolean prettyFormat) 方法, 示例 - String format_json = JSON.toJSONString(student, true);.net
Java Bean 對象 與 JSONObject 對象 互相轉化 :
-- Java Bean 對象 -> JSONObject 對象 : JSON.toJSON(Object javaObject) 方法, 示例 - JSONObject object = (JSONObject) JSON.toJSON(student_s);
-- JSONObject 對象 -> Java Bean 對象 (無直接方法實現) : JSON.parseObject(jsonObject.toString(), Class<T> clazz) 方法, 先轉成字符串, 再轉成 Java Bean;
json 字符串 與 JSONObject 對象互相轉化 :
-- json 字符串 -> JSONObject 對象 : JSON.parseObject(String text) 方法, 示例 - JSONObject object = JSON.parseObject(json_student);
-- JSONObject 對象 -> json 字符串 : JSONObject.toString() 方法, 示例 - object.toString();
Java Bean 對象 <--> json 字符串 <--> JSONArray 對象 互相轉化
json 字符串 與 Java 集合 互相轉化 :
-- json 字符串 -> Java 集合 : JSON.parseObject(String text, Class<Student> clazz) 方法, 示例 - List<Student> students = JSON.parseArray(json_array, Student.class);
-- Java 集合 -> json 字符串 : toJSONString(Object object, boolean prettyFormat) 方法, 示例 - String json_array = JSON.toJSONString(students, true);
Java 集合 與 JSONArray 對象 互相轉化 :
-- Java 集合 -> JSONArray 對象 : JSON.toJSON(Object javaObject) 方法, 示例 - JSONArray array = (JSONArray) JSON.toJSON(students);
-- JSONArray 對象 -> Java 集合 (沒有直接轉化方法) : 先轉成 json 字符串, 再轉成 Java 集合;
json 字符串 與 JSONArray 對象 互相轉化 :
-- json 字符串 -> JSONArray 對象 : JSON.parseArray(String text) 方法, 示例 - JSONArray array = JSON.parseArray(json_array);
-- JSONArray 對象 -> json 字符串 : jsonArray.toString() 方法;
一. JSON 簡介
1. JSON 結構
(1) 對象
對象簡介 :
-- 格式 : 對象定義在 大括號 {} 中, 中間的項由 逗號 隔開, 每一個項目都是 key : value 進行的;
-- 示例 : {"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1};
-- 格式化後的 :
- {
- "age":42,
- "id":1,
- "male":true,
- "name":"Sherlock Holmes",
- "schoolId":1
- }
(2) 數組
數組簡介 :
-- 數組格式 : 數組放在 中括號[] 中, 在 [] 中有 n 個 {} 元素, 每一個元素用 逗號 隔開;
-- 數組示例 : [{"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1},{"age":42,"id":2,"male":false,"name":"John Watson","schoolId":1}] ;
-- 輸出格式化後的數組 :
- [
- {
- "age":42,
- "id":1,
- "male":true,
- "name":"Sherlock Holmes",
- "schoolId":1
- },
- {
- "age":42,
- "id":2,
- "male":false,
- "name":"John Watson",
- "schoolId":1
- }
- ]
二. JSON 相關用法
1. JSON.toJSONString 方法
(1) JSON.toJSONString(Object object)方法簡介
JSON.toJSONString(Object object)方法簡介 :
-- 方法定義 : public static final String toJSONString(Object object) ;
-- 方法做用 : 將 java bean 轉爲 json 字符串;
-- 方法位置 : String com.alibaba.fastjson.JSON.toJSONString(Object object) ;
-- 參數介紹 : Object object -> 要轉換的實體類對象;
JSON.toJSONString(Object object, boolean prettyFormat)方法簡介 :
-- 方法定義 : public static final String toJSONString(Object object, boolean prettyFormat) ;
-- 方法做用 : 將 java bean 轉爲 json 字符串, 並將 json 字符串格式化;
-- 方法位置 : String com.alibaba.fastjson.JSON.toJSONString(Object object, boolean prettyFormat) ;
-- 參數介紹 : Object object -> 要轉換的實體類對象, boolean prettyFormat -> 是否將 json 字符串格式化 若是 false 與 上面的方法做用相同;
(2) JSON.toJSONString(Object object) 和 JSON.toJSONString(Object object, boolean prettyFormat)方法使用示例
JavaBean 類定義 :
- package cn.org.octopus.fastjson.beans;
-
- public class Student {
-
- private int id;
- private String name;
- private int age;
- private boolean isMale;
- private int schoolId;
-
- public Student() {
- super();
- }
-
- public Student(int id, String name, int age, boolean isMale, int schoolId) {
- super();
- this.id = id;
- this.name = name;
- this.age = age;
- this.isMale = isMale;
- this.schoolId = schoolId;
- }
-
- 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 int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public boolean isMale() {
- return isMale;
- }
-
- public void setMale(boolean isMale) {
- this.isMale = isMale;
- }
-
- public int getSchoolId() {
- return schoolId;
- }
-
- public void setSchoolId(int schoolId) {
- this.schoolId = schoolId;
- }
-
- @Override
- public String toString() {
- return "Student [id=" + id + ", name=" + name + ", age=" + age
- + ", isMale=" + isMale + ", schoolId=" + schoolId + "]";
- }
- }
main 函數代碼 :
- public static void main(String[] args) {
-
-
- Student student = new Student(1, "Sherlock Holmes", 42, true, 1);
-
- String json = JSON.toJSONString(student);
-
- System.out.println(json);
-
-
- String format_json = JSON.toJSONString(student, true);
-
- System.out.println(format_json);
-
- }
執行結果 :
-- 第一個結果 : 該結果沒有通過格式化, 只是將 json 字符串輸出;
-- 第二個結果 : 該結果按照 json 的格式進行格式化了;
- {"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1}
- {
- "age":42,
- "id":1,
- "male":true,
- "name":"Sherlock Holmes",
- "schoolId":1
- }
2. JSON.toJSON 方法
(1) JSON.toJSON(Object javaObject) 方法簡介
方法簡介 :
-- 方法定義 : public static final Object toJSON(Object javaObject) ;
-- 方法做用 : 將 java bean 對象轉爲 JSONObject 對象, 或者將 集合 轉爲 JSONArray 對象;
-- 方法位置 : Object com.alibaba.fastjson.JSON.toJSON(Object javaObject) ;
-- 參數介紹 : Object javaObject -> java bean 對象 或者 集合;
關鍵代碼解析 :
-- Java Bean 轉 JSONObject : JSONObject object = (JSONObject) JSON.toJSON(student_s);
-- Java 集合 轉 JSONArray : JSONArray array = (JSONArray) JSON.toJSON(students);
JSONOject 和 JSONArray 打印 : 這兩種對象 打印自己, toString() 以及 toJSONString() 都是相同的結果, 詳見下面的示例;
(2) 示例
Student 實體類 : 在 一. 1. (2) 中有定義, 直接使用該實體類便可;
Main() 示例 :
- public class Main {
-
-
- public static final String json_student = "{\"age\":42,\"id\":1,\"male\":true,\"name\":\"Sherlock Holmes\",\"schoolId\":1}";
-
- public static void main(String[] args) {
-
-
- Student student_s = new Student(1, "Sherlock Holmes", 42, true, 1);
-
- JSONObject object = (JSONObject) JSON.toJSON(student_s);
-
-
- System.out.println(object + " -- 打印 JSONOBject 自己");
- System.out.println(object.toString() + " -- 打印 JSONOBject.toString()");
- System.out.println(object.toJSONString() + " -- 打印 JSONOBject.toJSONString()");
-
-
- Student student_j = new Student(2, "John Watson", 42, false, 1);
-
- List<Student> students = new ArrayList<Student>();
- students.add(student_s);
- students.add(student_j);
-
- JSONArray array = (JSONArray) JSON.toJSON(students);
-
-
- System.out.println(array + " -- 打印 JSONArray 自己");
- System.out.println(array.toString() + " -- 打印 JSONArray.toString()");
- System.out.println(array.toJSONString() + " -- 打印 JSONArray.toJSONString()");
-
- }
執行結果 :
- {"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1} -- 打印 JSONOBject 自己
- {"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1} -- 打印 JSONOBject.toString()
- {"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1} -- 打印 JSONOBject.toJSONString()
- [{"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1},{"age":42,"id":2,"male":false,"name":"John Watson","schoolId":1}] -- 打印 JSONArray 自己
- [{"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1},{"age":42,"id":2,"male":false,"name":"John Watson","schoolId":1}] -- 打印 JSONArray.toString()
- [{"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1},{"age":42,"id":2,"male":false,"name":"John Watson","schoolId":1}] -- 打印 JSONArray.toJSONString()
3. JSON.parseArray 方法
(1) JSON.parseArray 方法簡介
JSONArray parseArray(String text)方法簡介 :
-- 方法定義 : public static final JSONArray parseArray(String text) ;
-- 方法做用 : 將 json 字符串轉爲 JSONArray 對象;
-- 方法位置 : JSONArray com.alibaba.fastjson.JSON.parseArray(String text) ;
-- 參數介紹 : String text -> json 字符串 ;
<T> List<T> parseArray(String text, Class<T> clazz) 方法簡介 :
-- 方法定義 : public static final <T> List<T> parseArray(String text, Class<T> clazz) ;
-- 方法做用 : 將 json 字符串轉爲 List 集合;
-- 方法位置 : <T> List<T> com.alibaba.fastjson.JSON.parseArray(String text, Class<T> clazz) ;
-- 參數介紹 : String text -> json 字符串, Class<T> clazz -> 集合元素類型;
(2) 代碼示例
Main() 代碼 :
- public static final String json_array = "[{\"age\":42,\"id\":1,\"male\":true,\"name\":\"Sherlock Holmes\",\"schoolId\":1},{\"age\":42,\"id\":2,\"male\":false,\"name\":\"John Watson\",\"schoolId\":1}]";
-
- public static void main(String[] args) {
-
-
- JSONArray array = JSON.parseArray(json_array);
-
- System.out.println(array);
-
-
- List<Student> students = JSON.parseArray(json_array, Student.class);
-
- System.out.println("students.size() : " + students.size());
-
- for(Student student : students)
- System.out.println(student.toString());
-
- }
執行結果 :
- [{"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1},{"age":42,"id":2,"male":false,"name":"John Watson","schoolId":1}]
- students.size() : 2
- Student [id=1, name=Sherlock Holmes, age=42, isMale=true, schoolId=1]
- Student [id=2, name=John Watson, age=42, isMale=false, schoolId=1]
4. JSON.parseObject 方法
(1) JSON.parseObject 方法簡介
JSONObject parseObject(String text) 方法簡介 :
-- 方法定義 : public static final JSONObject parseObject(String text);
-- 方法做用 : 將 json 字符串類型轉化爲 JSONObject 對象;
-- 方法位置 : JSONObject com.alibaba.fastjson.JSON.parseObject(String text);
-- 參數介紹 : String text -> json 字符串;
<T> T parseObject(String text, Class<T> clazz) 方法簡介 :
-- 方法定義 : public static final <T> T parseObject(String text, Class<T> clazz);
-- 方法做用 : 將 json 字符串轉爲指定類型的 java bean 對象;
-- 方法位置 : <T> T com.alibaba.fastjson.JSON.parseObject(String text, Class<T> clazz);
-- 參數介紹 : String text -> json 字符串, Class<T> clazz -> 要轉化的 java bean 對象類型;
(2) 方法示例
Main() 方法 :
- public static final String json_student = "{\"age\":42,\"id\":1,\"male\":true,\"name\":\"Sherlock Holmes\",\"schoolId\":1}";
-
- public static void main(String[] args) {
-
-
- JSONObject object = JSON.parseObject(json_student);
-
- System.out.println(object);
-
-
- Student student = JSON.parseObject(json_student, Student.class);
-
- System.out.println(student.toString());
-
- }
執行結果 :
- {"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1}
- Student [id=1, name=Sherlock Holmes, age=42, isMale=true, schoolId=1]
5. JSON.parse 方法
(1) Object parse(String text) 方法簡介
方法簡介 :
-- 方法定義 : public static final Object parse(String text);
-- 方法做用 : 將 json 字符串轉爲 JSONObject 或者 JSONArray 對象;
-- 方法位置 : Object com.alibaba.fastjson.JSON.parse(String text);
-- 參數介紹 : String text -> json 字符串;
(2) 代碼示例
Main() 方法 :
- public static final String json_student = "{\"age\":42,\"id\":1,\"male\":true,\"name\":\"Sherlock Holmes\",\"schoolId\":1}";
-
- public static void main(String[] args) {
-
-
- JSONObject object = (JSONObject) JSON.parse(json_student);
-
- System.out.println(object);
-
-
- JSONArray array = (JSONArray) JSON.parse(json_array);
-
- System.out.println(array);
-
- }
運行結果 :
- {"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1}
- [{"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1},{"age":42,"id":2,"male":false,"name":"John Watson","schoolId":1}]
三. JSONObject 相關用法
1. JSONObject 方法介紹
相關方法介紹 :
-- 獲取 JSONObject 中元素 : 獲取 String 就調用 jsonObject.getString("key"), 獲取 boolean 調用 jsonObject.getBoolean("key"), 同理還能夠獲取其它類型的數據;
-- 向 JSONObject 中添加元素 : object.put("action", "fuck");
-- 向 JSONObject 中添加多個元素 : 將一個 Map 集合添加到 JSONObject 中, Map 集合的鍵必須是 String 類型;
- Map<String, String> map = new HashMap<String, String>();
- map.put("home", "American");
- map.put("school", "harvard");
- object.putAll(map);
-- 移除 JSONObject 中的屬性 : jsonObject.remove("key"), 參數是 鍵;
-- 獲取 JSONObject 中元素個數 : jsonObject.size();
2. 代碼示例
Main() 函數代碼 :
- public static void main(String[] args) {
-
-
- Student student = new Student(1, "Sherlock Holmes", 42, true, 1);
-
- JSONObject object = (JSONObject) JSON.toJSON(student);
-
- System.out.println(object.toString());
-
-
- String age = object.getString("age");
- boolean isMale = object.getBoolean("male");
- System.out.println("age : " + age + " , male : " + isMale);
-
-
- object.put("action", "fuck");
- System.out.println(object.toString());
-
-
- Map<String, String> map = new HashMap<String, String>();
- map.put("home", "American");
- map.put("school", "harvard");
- object.putAll(map);
- System.out.println(object.toJSONString());
-
-
- object.remove("action");
- System.out.println(object);
-
-
- System.out.println(object.size());
-
- }
執行結果 :
- {"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1}
- age : 42 , male : true
- {"action":"fuck","age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1}
- {"action":"fuck","age":42,"home":"American","id":1,"male":true,"name":"Sherlock Holmes","school":"harvard","schoolId":1}
- {"age":42,"home":"American","id":1,"male":true,"name":"Sherlock Holmes","school":"harvard","schoolId":1}
- 7
四. 全部相關代碼示例
1. Java Bean 類
- package cn.org.octopus.fastjson.beans;
-
- public class Student {
-
- private int id;
- private String name;
- private int age;
- private boolean isMale;
- private int schoolId;
-
- public Student() {
- super();
- }
-
- public Student(int id, String name, int age, boolean isMale, int schoolId) {
- super();
- this.id = id;
- this.name = name;
- this.age = age;
- this.isMale = isMale;
- this.schoolId = schoolId;
- }
-
- 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 int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public boolean isMale() {
- return isMale;
- }
-
- public void setMale(boolean isMale) {
- this.isMale = isMale;
- }
-
- public int getSchoolId() {
- return schoolId;
- }
-
- public void setSchoolId(int schoolId) {
- this.schoolId = schoolId;
- }
-
- @Override
- public String toString() {
- return "Student [id=" + id + ", name=" + name + ", age=" + age
- + ", isMale=" + isMale + ", schoolId=" + schoolId + "]";
- }
- }
2. JSON 相關方法 Main 示例
- package cn.org.octopus.fastjson;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import cn.org.octopus.fastjson.beans.Student;
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
-
- public class Main {
-
-
-
- public static final String json_array = "[{\"age\":42,\"id\":1,\"male\":true,\"name\":\"Sherlock Holmes\",\"schoolId\":1},{\"age\":42,\"id\":2,\"male\":false,\"name\":\"John Watson\",\"schoolId\":1}]";
-
-
- public static final String json_student = "{\"age\":42,\"id\":1,\"male\":true,\"name\":\"Sherlock Holmes\",\"schoolId\":1}";
-
- public static void main(String[] args) {
-
-
- }
-
-
-
-
- private static void JSONArrayMethod() {
-
- Student student_s = new Student(1, "Sherlock Holmes", 42, true, 1);
-
- Student student_j = new Student(2, "John Watson", 42, false, 1);
-
- List<Student> students = new ArrayList<Student>();
- students.add(student_s);
- students.add(student_j);
-
-
- JSONArray array = (JSONArray) JSON.toJSON(students);
- System.out.println("List -> JSONArray : " + array);
-
-
- String json_array = JSON.toJSONString(students, true);
- System.out.println("List -> json Str" + json_array);
-
-
- List<Student> students1 = JSON.parseArray(json_array, Student.class);
- System.out.println("json Str -> List : " + students1);
-
-
- JSONArray array1 = JSON.parseArray(json_array);
- System.out.println("json Str -> JSONArray : " + array1);
- }
-
-
-
-
- private static void JSONObjectMethod() {
-
- Student student = new Student(1, "Sherlock Holmes", 42, true, 1);
-
- JSONObject object = (JSONObject) JSON.toJSON(student);
-
- System.out.println(object.toString());
-
-
- String age = object.getString("age");
- boolean isMale = object.getBoolean("male");
- System.out.println("age : " + age + " , male : " + isMale);
-
-
- object.put("action", "fuck");
- System.out.println(object.toString());
-
-
- Map<String, String> map = new HashMap<String, String>();
- map.put("home", "American");
- map.put("school", "harvard");
- object.putAll(map);
- System.out.println(object.toJSONString());
-
-
- object.remove("action");
- System.out.println(object);
-
-
- System.out.println(object.size());
- }
-
-
-
-
- private static void parseMethod() {
-
- JSONObject object = (JSONObject) JSON.parse(json_student);
-
- System.out.println(object);
-
-
- JSONArray array = (JSONArray) JSON.parse(json_array);
-
- System.out.println(array);
- }
-
-
-
-
- private static void parseObjectMethod() {
-
- JSONObject object = JSON.parseObject(json_student);
-
- System.out.println(object);
-
-
- Student student = JSON.parseObject(json_student, Student.class);
-
- System.out.println(student.toString());
- }
-
-
-
-
- private static void parseArrayMethod() {
-
- JSONArray array = JSON.parseArray(json_array);
-
- System.out.println(array);
-
-
- List<Student> students = JSON.parseArray(json_array, Student.class);
-
- System.out.println("students.size() : " + students.size());
-
- for(Student student : students)
- System.out.println(student.toString());
- }
-
-
-
-
- private static void toJSONMethod() {
-
- Student student_s = new Student(1, "Sherlock Holmes", 42, true, 1);
-
- JSONObject object = (JSONObject) JSON.toJSON(student_s);
-
-
- System.out.println(object + " -- 打印 JSONOBject 自己");
- System.out.println(object.toString() + " -- 打印 JSONOBject.toString()");
- System.out.println(object.toJSONString() + " -- 打印 JSONOBject.toJSONString()");
-
-
- Student student_j = new Student(2, "John Watson", 42, false, 1);
-
- List<Student> students = new ArrayList<Student>();
- students.add(student_s);
- students.add(student_j);
-
- JSONArray array = (JSONArray) JSON.toJSON(students);
-
- String format_json = JSON.toJSONString(students, true);
- System.out.println(format_json);
-
-
- System.out.println(array + " -- 打印 JSONArray 自己");
- System.out.println(array.toString() + " -- 打印 JSONArray.toString()");
- System.out.println(array.toJSONString() + " -- 打印 JSONArray.toJSONString()");
- }
-
-
- private static void toJsonStringMethod() {
-
- Student student = new Student(1, "Sherlock Holmes", 42, true, 1);
-
- String json = JSON.toJSONString(student);
-
- System.out.println(json);
-
-
- String format_json = JSON.toJSONString(student, true);
-
- System.out.println(format_json);
- }
-
- }