fastJson對(duì)于json格式字符串的解析主要用到了一下三個(gè)類:
1.JSON:fastJson的解析器,用于JSON格式字符串與JSON對(duì)象及javaBean之間的轉(zhuǎn)換晶默。
2.JSONObject:fastJson提供的json對(duì)象屎债。
3.JSONArray:fastJson提供json數(shù)組對(duì)象。
我們可以把JSONObject當(dāng)成一個(gè)Map<String,Object>來(lái)看廉丽,只是JSONObject提供了更為豐富便捷的方法,方便我們對(duì)于對(duì)象屬性的操作。我們看一下源碼栖博。
同樣我們可以把JSONArray當(dāng)做一個(gè)List<Object>,可以把JSONArray看成JSONObject對(duì)象的一個(gè)集合。
此外雹舀,由于JSONObject和JSONArray繼承了JSON,所以說(shuō)也可以直接使用兩者對(duì)JSON格式字符串與JSON對(duì)象及javaBean之間做轉(zhuǎn)換,不過(guò)為了避免混淆我們還是使用JSON。
首先定義三個(gè)json格式的字符串捉腥,作為我們的數(shù)據(jù)源。
//json字符串-簡(jiǎn)單對(duì)象型
private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
//json字符串-數(shù)組類型
private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
//復(fù)雜格式j(luò)son字符串
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對(duì)象之間的轉(zhuǎn)換粒竖。
示例1.1-json字符串-簡(jiǎn)單對(duì)象型與JSONObject之間的轉(zhuǎn)換
/**
* json字符串-簡(jiǎn)單對(duì)象型與JSONObject之間的轉(zhuǎn)換
*/
public static void testJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
//JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因?yàn)镴SONObject繼承了JSON喉刘,所以這樣也是可以的
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
示例1.2-json字符串-數(shù)組類型與JSONArray之間的轉(zhuǎn)換
/**
* json字符串-數(shù)組類型與JSONArray之間的轉(zhuǎn)換
*/
public static void testJSONStrToJSONArray(){
JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
//JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因?yàn)镴SONArray繼承了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-復(fù)雜json格式字符串與JSONObject之間的轉(zhuǎn)換
/**
* 復(fù)雜json格式字符串與JSONObject之間的轉(zhuǎn)換
*/
public static void testComplexJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
//JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因?yàn)镴SONObject繼承了JSON他挎,所以這樣也是可以的
String teacherName = jsonObject.getString("teacherName");
Integer teacherAge = jsonObject.getInteger("teacherAge");
JSONObject course = jsonObject.getJSONObject("course");
JSONArray students = jsonObject.getJSONArray("students");
}
示例2:JSON格式字符串與javaBean之間的轉(zhuǎn)換损姜。
首先顾孽,我們針對(duì)數(shù)據(jù)源所示的字符串灾常,提供三個(gè)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之間的轉(zhuǎn)換推薦使用 TypeReference<T> 這個(gè)類凰盔,使用泛型可以更加清晰抄瑟,當(dāng)然也有其它的轉(zhuǎn)換方式,這里就不做探討了哮笆。
示例2.1-json字符串-簡(jiǎn)單對(duì)象型與javaBean之間的轉(zhuǎn)換
/**
* json字符串-簡(jiǎn)單對(duì)象與JavaBean_obj之間的轉(zhuǎn)換
*/
public static void testJSONStrToJavaBeanObj(){
Student student = JSON.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});
//Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});//因?yàn)镴SONObject繼承了JSON,所以這樣也是可以的
System.out.println(student.getStudentName()+":"+student.getStudentAge());
}
示例2.2-json字符串-數(shù)組類型與javaBean之間的轉(zhuǎn)換
/**
* json字符串-數(shù)組類型與JavaBean_List之間的轉(zhuǎn)換
*/
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>>() {});//因?yàn)镴SONArray繼承了JSON,所以這樣也是可以的
for (Student student : students) {
System.out.println(student.getStudentName()+":"+student.getStudentAge());
}
}
示例2.3-復(fù)雜json格式字符串與與javaBean之間的轉(zhuǎn)換
/**
* 復(fù)雜json格式字符串與JavaBean_obj之間的轉(zhuǎn)換
*/
public static void testComplexJSONStrToJavaBean(){
Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});
//Teacher teacher1 = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});//因?yàn)镴SONObject繼承了JSON漏策,所以這樣也是可以的
String teacherName = teacher.getTeacherName();
Integer teacherAge = teacher.getTeacherAge();
Course course = teacher.getCourse();
List<Student> students = teacher.getStudents();
}
對(duì)于TypeReference<T>感耙,由于其構(gòu)造方法使用 protected 進(jìn)行修飾,所以在其他包下創(chuàng)建其對(duì)象的時(shí)候层皱,要用其實(shí)現(xiàn)類的子類:new TypeReference<Teacher>() {}
此外的:
1哩俭,對(duì)于JSON對(duì)象與JSON格式字符串的轉(zhuǎn)換可以直接用 toJSONString()這個(gè)方法。
2薛闪,javaBean與JSON格式字符串之間的轉(zhuǎn)換要用到:JSON.toJSONString(obj);
3辛馆,javaBean與json對(duì)象間的轉(zhuǎn)換使用:JSON.toJSON(obj),然后使用強(qiáng)制類型轉(zhuǎn)換豁延,JSONObject或者JSONArray昙篙。