fastjson對于json格式字符串的解析主要用到了一下三個類:
json:fastjson的解析器,用于json格式字符串與json對象及javabean之間的轉換。
jsonobject:fastjson提供的json對象。
jsonarray:fastjson提供json數組對象。
我們可以把jsonobject當成一個map<string,object>來看,只是jsonobject提供了更為豐富便捷的方法,方便我們對于對象屬性的操作。我們看一下源碼。
同樣我們可以把jsonarray當做一個list<object>,可以把jsonarray看成jsonobject對象的一個集合。
此外,由于jsonobject和jsonarray繼承了json,所以說也可以直接使用兩者對json格式字符串與json對象及javabean之間做轉換,不過為了避免混淆我們還是使用json。
首先定義三個json格式的字符串,作為我們的數據源。
1
2
3
4
5
6
|
//json字符串-簡單對象型 private static final string json_obj_str = "{\"studentname\":\"lily\",\"studentage\":12}" ; //json字符串-數組類型 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對象之間的轉換。
示例1.1-json字符串-簡單對象型與jsonobject之間的轉換
1
2
3
4
5
6
7
8
|
/** * json字符串-簡單對象型與jsonobject之間的轉換 */ 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字符串-數組類型與jsonarray之間的轉換
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/** * json字符串-數組類型與jsonarray之間的轉換 */ 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之間的轉換
1
2
3
4
5
6
7
8
9
10
11
|
/** * 復雜json格式字符串與jsonobject之間的轉換 */ 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之間的轉換。
首先,我們針對數據源所示的字符串,提供三個javabean。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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之間的轉換推薦使用 typereference<t> 這個類,使用泛型可以更加清晰,當然也有其它的轉換方式,這里就不做探討了。
示例2.1-json字符串-簡單對象型與javabean之間的轉換
1
2
3
4
5
6
7
8
|
/** * json字符串-簡單對象與javabean_obj之間的轉換 */ 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字符串-數組類型與javabean之間的轉換
1
2
3
4
5
6
7
8
9
10
|
/** * json字符串-數組類型與javabean_list之間的轉換 */ 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之間的轉換
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * 復雜json格式字符串與javabean_obj之間的轉換 */ 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 進行修飾,所以在其他包下創建其對象的時候,要用其實現類的子類:new typereference<teacher>() {}
此外的:
1,對于json對象與json格式字符串的轉換可以直接用 tojsonstring()這個方法。
2,javabean與json格式字符串之間的轉換要用到:json.tojsonstring(obj);
3,javabean與json對象間的轉換使用:json.tojson(obj),然后使用強制類型轉換,jsonobject或者jsonarray。
最后說一點,我們作為程序員,研究問題還是要仔細深入一點的。當你對原理了解的有夠透徹,開發起來也就得心應手了,很多開發中的問題和疑惑也就迎刃而解了,而且在面對其他問題的時候也可做到觸類旁通。當然在開發中沒有太多的時間讓你去研究原理,開發中要以實現功能為前提,可等項目上線的后,你有大把的時間或者空余的時間,你大可去刨根問底,深入的去研究一項技術,為覺得這對一名程序員的成長是很重要的事情。
總結
以上所述是小編給大家介紹的fastjson對于json格式字符串、json對象及javabean之間的相互轉換,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/cdf-opensource-007/p/7106018.html