fastJson對于json格式字符串的解析主要用到了一下三個類:
創(chuàng)新互聯(lián) - 成都棕樹機房,四川服務器租用,成都服務器租用,四川網(wǎng)通托管,綿陽服務器托管,德陽服務器托管,遂寧服務器托管,綿陽服務器托管,四川云主機,成都云主機,西南云主機,成都棕樹機房,西南服務器托管,四川/成都大帶寬,機柜大帶寬,四川老牌IDC服務商
JSON:fastJson的解析器,用于JSON格式字符串與JSON對象及javaBean之間的轉(zhuǎn)換。
JSONObject:fastJson提供的json對象。
JSONArray:fastJson提供json數(shù)組對象。
我們可以把JSONObject當成一個Map<String,Object>來看,只是JSONObject提供了更為豐富便捷的方法,方便我們對于對象屬性的操作。我們看一下源碼。

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

此外,由于JSONObject和JSONArray繼承了JSON,所以說也可以直接使用兩者對JSON格式字符串與JSON對象及javaBean之間做轉(zhuǎn)換,不過為了避免混淆我們還是使用JSON。
首先定義三個json格式的字符串,作為我們的數(shù)據(jù)源。
//json字符串-簡單對象型
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}]";
//復雜格式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對象之間的轉(zhuǎn)換。
示例1.1-json字符串-簡單對象型與JSONObject之間的轉(zhuǎn)換
/**
* json字符串-簡單對象型與JSONObject之間的轉(zhuǎn)換
*/
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字符串-數(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);//因為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之間的轉(zhuǎn)換
/**
* 復雜json格式字符串與JSONObject之間的轉(zhuǎn)換
*/
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之間的轉(zhuǎn)換。
首先,我們針對數(shù)據(jù)源所示的字符串,提供三個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> 這個類,使用泛型可以更加清晰,當然也有其它的轉(zhuǎn)換方式,這里就不做探討了。
示例2.1-json字符串-簡單對象型與javaBean之間的轉(zhuǎn)換
/**
* json字符串-簡單對象與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>() {});//因為JSONObject繼承了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>>() {});//因為JSONArray繼承了JSON,所以這樣也是可以的
for (Student student : students) {
System.out.println(student.getStudentName()+":"+student.getStudentAge());
}
}示例2.3-復雜json格式字符串與與javaBean之間的轉(zhuǎn)換
/**
* 復雜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>() {});//因為JSONObject繼承了JSON,所以這樣也是可以的
String teacherName = teacher.getTeacherName();
Integer teacherAge = teacher.getTeacherAge();
Course course = teacher.getCourse();
List<Student> students = teacher.getStudents();
}對于TypeReference<T>,由于其構造方法使用 protected 進行修飾,所以在其他包下創(chuàng)建其對象的時候,要用其實現(xiàn)類的子類:new TypeReference<Teacher>() {}

此外的:
1,對于JSON對象與JSON格式字符串的轉(zhuǎn)換可以直接用 toJSONString()這個方法。
2,javaBean與JSON格式字符串之間的轉(zhuǎn)換要用到:JSON.toJSONString(obj);
3,javaBean與json對象間的轉(zhuǎn)換使用:JSON.toJSON(obj),然后使用強制類型轉(zhuǎn)換,JSONObject或者JSONArray。
最后說一點,我們作為程序員,研究問題還是要仔細深入一點的。當你對原理了解的有夠透徹,開發(fā)起來也就得心應手了,很多開發(fā)中的問題和疑惑也就迎刃而解了,而且在面對其他問題的時候也可做到觸類旁通。當然在開發(fā)中沒有太多的時間讓你去研究原理,開發(fā)中要以實現(xiàn)功能為前提,可等項目上線的后,你有大把的時間或者空余的時間,你大可去刨根問底,深入的去研究一項技術,為覺得這對一名程序員的成長是很重要的事情。
總結(jié)
以上所述是小編給大家介紹的FastJson對于JSON格式字符串、JSON對象及JavaBean之間的相互轉(zhuǎn)換,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
本文題目:FastJson對于JSON格式字符串、JSON對象及JavaBean之間的相互轉(zhuǎn)換操作
網(wǎng)頁URL:http://chinadenli.net/article0/geceoo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、手機網(wǎng)站建設、網(wǎng)站策劃、網(wǎng)站制作、移動網(wǎng)站建設、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)