FastJson對(duì)于JSON格式字符串、JSON對(duì)象及JavaBean之間的相互轉(zhuǎn)換操作
fastJson對(duì)于json格式字符串的解析主要用到了一下三個(gè)類:
JSON:fastJson的解析器,用于JSON格式字符串與JSON對(duì)象及javaBean之間的轉(zhuǎn)換。
JSONObject:fastJson提供的json對(duì)象。
JSONArray:fastJson提供json數(shù)組對(duì)象。
我們可以把JSONObject當(dāng)成一個(gè)Map<String,Object>來看,只是JSONObject提供了更為豐富便捷的方法,方便我們對(duì)于對(duì)象屬性的操作。我們看一下源碼。

同樣我們可以把JSONArray當(dāng)做一個(gè)List<Object>,可以把JSONArray看成JSONObject對(duì)象的一個(gè)集合。

此外,由于JSONObject和JSONArray繼承了JSON,所以說也可以直接使用兩者對(duì)JSON格式字符串與JSON對(duì)象及javaBean之間做轉(zhuǎn)換,不過為了避免混淆我們還是使用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。
最后說一點(diǎn),我們作為程序員,研究問題還是要仔細(xì)深入一點(diǎn)的。當(dāng)你對(duì)原理了解的有夠透徹,開發(fā)起來也就得心應(yīng)手了,很多開發(fā)中的問題和疑惑也就迎刃而解了,而且在面對(duì)其他問題的時(shí)候也可做到觸類旁通。當(dāng)然在開發(fā)中沒有太多的時(shí)間讓你去研究原理,開發(fā)中要以實(shí)現(xiàn)功能為前提,可等項(xiàng)目上線的后,你有大把的時(shí)間或者空余的時(shí)間,你大可去刨根問底,深入的去研究一項(xiàng)技術(shù),為覺得這對(duì)一名程序員的成長(zhǎng)是很重要的事情。
總結(jié)
以上所述是小編給大家介紹的FastJson對(duì)于JSON格式字符串、JSON對(duì)象及JavaBean之間的相互轉(zhuǎn)換,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
RocketMq同組消費(fèi)者如何自動(dòng)設(shè)置InstanceName
這篇文章主要介紹了RocketMq同組消費(fèi)者如何自動(dòng)設(shè)置InstanceName問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Java調(diào)用ChatGPT(基于SpringBoot和Vue)實(shí)現(xiàn)可連續(xù)對(duì)話和流式輸出的ChatGPT API
這篇文章主要介紹了Java調(diào)用ChatGPT(基于SpringBoot和Vue),實(shí)現(xiàn)可連續(xù)對(duì)話和流式輸出的ChatGPT API(可自定義實(shí)現(xiàn)AI助手),文中代碼示例介紹的非常詳細(xì),感興趣的朋友可以參考下2023-04-04
java實(shí)戰(zhàn)案例之用戶注冊(cè)并發(fā)送郵件激活/發(fā)送郵件驗(yàn)證碼
現(xiàn)在很多的網(wǎng)站都提供有用戶注冊(cè)功能,當(dāng)我們注冊(cè)成功之后就會(huì)收到封注冊(cè)網(wǎng)站的郵件,郵件里包含了我們的注冊(cè)的用戶名和密碼及激活賬戶的超鏈接等信息,這篇文章主要給大家介紹了關(guān)于java實(shí)戰(zhàn)案例之用戶注冊(cè)并發(fā)送郵件激活/發(fā)送郵件驗(yàn)證碼的相關(guān)資料,需要的朋友可以參考下2021-09-09
Spring boot 跳轉(zhuǎn)到j(luò)sp頁面的實(shí)現(xiàn)方法
本篇文章主要介紹了Spring boot 跳轉(zhuǎn)到j(luò)sp頁面的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Java異常處理中同時(shí)有finally和return語句的執(zhí)行問題
這篇文章主要介紹了Java異常處理中同時(shí)有finally和return語句的執(zhí)行問題,首先確定的是一般finally語句都會(huì)被執(zhí)行...然后,需要的朋友可以參考下2015-11-11
SpringBoot Actuator潛在的OOM問題的解決
本文主要介紹了SpringBoot Actuator潛在的OOM問題的解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
MybatisPlus使用排序查詢時(shí)將null值放到最后
按照更新時(shí)間排序,但是更新時(shí)間可能為null,因此將null的數(shù)據(jù)放到最后,本文主要介紹了MybatisPlus使用排序查詢時(shí)將null值放到最后,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08

