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() 代码 :
//字符串内容 [{"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_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) { //将 json 字符串转为 JSONArray 对象 JSONArray array = JSON.parseArray(json_array); //打印 JSONArray 对象 System.out.println(array); //将 json 字符串转为 List 集合 List<Student> students = JSON.parseArray(json_array, Student.class); //打印 List 集合大小 System.out.println("students.size() : " + students.size()); //遍历 List 集合中的元素 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() 方法 :
//该字符串是 {"age":42,"id":1,"male":true,"name":"Sherlock Holmes","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) { //将 json 字符串转为 JSONObject 对象 JSONObject object = JSON.parseObject(json_student); //打印 JSONObject 对象 System.out.println(object); //将 json 字符串转为 Student 对象 Student student = JSON.parseObject(json_student, Student.class); //打印 Student 对象 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() 方法 :
//该字符串是 {"age":42,"id":1,"male":true,"name":"Sherlock Holmes","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) { //将 json 字符串转为 JSONObect 对象 JSONObject object = (JSONObject) JSON.parse(json_student); //打印 JSONObject 对象 System.out.println(object); //将 json 字符串 转为 JSONArray 对象 JSONArray array = (JSONArray) JSON.parse(json_array); //打印 JSONArray 对象 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 类型;
//向 JSONObject 对象中添加多个属性
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 student = new Student(1, "Sherlock Holmes", 42, true, 1); //将 Java Bean 对象转为 JSONObject 对象 JSONObject object = (JSONObject) JSON.toJSON(student); //打印 JSONObject 对象 System.out.println(object.toString()); //获取 JSONObject 对象的 age 和 male 值 String age = object.getString("age"); boolean isMale = object.getBoolean("male"); System.out.println("age : " + age + " , male : " + isMale); //向 JSONObject 对象中添加单个属性 object.put("action", "fuck"); System.out.println(object.toString()); //向 JSONObject 对象中添加多个属性 Map<String, String> map = new HashMap<String, String>(); map.put("home", "American"); map.put("school", "harvard"); object.putAll(map); System.out.println(object.toJSONString()); //移除 JSONObject 对象中的属性 object.remove("action"); System.out.println(object); //获取 JSONObject 中的属性个数 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 示例