久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Gson詳細(xì)使用,,此Gson非彼Json,你值得擁有~

 昵稱35402089 2016-07-30

額~先來一句官方的話

Gson是谷歌推出的解析json數(shù)據(jù)以及將對象轉(zhuǎn)換成json數(shù)據(jù)的一個開源框架. 現(xiàn)在json因其易讀性和高效率而被廣泛的使用著. 相對于java以及其它json的解析框架,Gson非常的好用.

熟悉網(wǎng)絡(luò)編程的人知道,,網(wǎng)絡(luò)請求返回的數(shù)據(jù)格式一般要么是json,要么是xml,但是采用json格式這種方式是使用的最為廣泛的,。
網(wǎng)絡(luò)請求返回的json數(shù)據(jù)時,,使用JSONObject或者JSONArray來承載數(shù)據(jù),然后把返回的數(shù)據(jù)當(dāng)作一個字典,,根據(jù)鍵取出相應(yīng)的值。

現(xiàn)在假如網(wǎng)絡(luò)請求返回這樣一個數(shù)據(jù):
{
“city”: “北京”,
“cityId”: “10000000001”,
“temp”: “24”,
“wd”: “南風(fēng)”,
“ws”: “2級”,
“sd”: “74%”,
“wse”: “2”,
“time”: “17:45”,
“isReader”: “1”,
“Radar”: “JC_RADAR_AZ9010_JB”,
“njd”: “暫無實況\t”,
“qy”: “1005”
}
那么在使用Json在解析這段數(shù)據(jù)的時候是依據(jù)每一個Key來取值的,。
那我們要一次知道如下一些鍵值key才能取到相應(yīng)的value.
city ,cityId,temp,wd,ws,sd,wse,time,isReader,Radar;
如果在做項目的時候后臺給的接口不是很多,,那么采用這種方式還是可以接受的,如果后臺接口沒有設(shè)計好,,可能要改變幾個接口,,增添若干個字段,或刪除若干個字段,,那么對于前端開發(fā)人員來說這是難以接受的,,而且對于整個項目來說也是非常難以接受,對于這樣的后臺開發(fā)者來說,,我個人覺得那是要拖出來槍斃的,因為接口一改變,,對于大一點的項目來說,Android,,IOS,Web,微信等等前端開發(fā),,都需要進(jìn)行相應(yīng)的改變,需要改變重新調(diào)整數(shù)據(jù)實體和修改相應(yīng)json解析,,如果項目很大的話,,即便修改一點點也是非常耗時間的。曾經(jīng)年少無知做過這樣的項目,,還好和自己的好朋友同學(xué)一起開發(fā),,不過也因此發(fā)生過嚴(yán)重的爭執(zhí),感謝自己的當(dāng)時的忍耐,,沒有打起來,。不過現(xiàn)在仍然是好朋友^_^.如果當(dāng)時要是知道了有Gson這吊炸天的玩意兒,也許就不會發(fā)生那么多不愉快的事情了,,當(dāng)然現(xiàn)在不僅僅只有Gson,,還有fastJson也有同樣的功能,好吧,,下面我們來看看Gson是如何的吊炸天,。

在此之前,我們需要準(zhǔn)備gson.jar
可以到官網(wǎng)下載:
http:///artifact/com.google.code.gson/gson
這里寫圖片描述
點擊下載
這里寫圖片描述
我下載的是最新的,,2.6.2版本的,鏈接不上的也可以直接在這里下載

好了現(xiàn)在開始講解介紹gson了
1.在調(diào)用JSON操作時,,gson實例不維護(hù)任何狀態(tài),。所以,你可以重復(fù)使用同一對象的多個JSON序列化和反序列化操作,。
(Serialization)序列化操作如下:

import com.google.gson.Gson;public class Demo04 { public static void main(String[] args) { Gson gson = new Gson(); String s = gson.toJson(11111); System.out.println('(11111)->'+s); s = gson.toJson('abcdefg'); System.out.println('(abcd)->'+s); s = gson.toJson(new Long(10)); System.out.println('(10)->'+s); int[] values = { 1 ,2,3,4,5,6}; s = gson.toJson(values); System.out.println('(1 ,2,3,4,5,6)->'+s); }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

控制臺輸出:
(11111)->11111
(abcd)->”abcdefg”
(10)->10
(1 ,2,3,4,5,6)->[1,2,3,4,5,6]


(Deserialization)反序列化操作如下:

import java.util.List;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;public class Demo05 { public static void main(String[] args) { // TODO Auto-generated method stub Gson gson = new Gson(); int one = gson.fromJson('1', int.class); System.out.println('one:' + one); Integer integer = gson.fromJson('1', Integer.class); System.out.println('integer:' + integer); Long long1 = gson.fromJson('1', Long.class); System.out.println('long1:' + long1); Boolean boolean1 = gson.fromJson('false', Boolean.class); System.out.println('boolean1:' + boolean1); String str = gson.fromJson('\'abc\'', String.class); System.out.println('str:' + str); List list = gson.fromJson('[\'abc\',\'def\']', new TypeToken<>>() { }.getType()); System.out.println('list:' + list); }}
  • 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

控制臺輸出:
one:1
integer:1
long1:1
boolean1:false
str:abc
list:[abc, def]

2.(對象例子)Object Examples
實體如下:

package domain;import java.util.Date;public class Student { private int id; private String name; private Date birthDay; public Student(){} public Student(int id, String name, Date birthDay) { super(); this.id = id; this.name = name; this.birthDay = birthDay; } 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 Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } public String toString() { return 'Student [birthDay=' + birthDay + ', id=' + id + ', name=' + name + ']'; }}
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

(Serialization)序列化操作如下:

import com.google.gson.Gson;public class Demo03 { public static void main(String[] args) { // TODO Auto-generated method stub Weather w = new Weather('北京', '10000000001', '24', '南風(fēng)', '2級', '74%', '2', '17:45', '1', 'JC_RADAR_AZ9010_JB', '暫無實況 ', '1005'); Gson gson = new Gson(); System.out.println(gson.toJson(w)); }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

控制臺輸出(手動轉(zhuǎn)成Json格式):
{
“city”: “北京”,
“cityId”: “10000000001”,
“temp”: “24”,
“wd”: “南風(fēng)”,
“ws”: “2級”,
“sd”: “74%”,
“wse”: “2”,
“time”: “17:45”,
“isReader”: “1”,
“Radar”: “JC_RADAR_AZ9010_JB”,
“njd”: “暫無實況\t”,
“qy”: “1005”
}
(Deserialization)反序列化操作如下:

import com.google.gson.Gson;public class Demo03 { public static void main(String[] args) { // TODO Auto-generated method stub Weather w = new Weather('北京', '10000000001', '24', '南風(fēng)', '2級', '74%', '2', '17:45', '1', 'JC_RADAR_AZ9010_JB', '暫無實況 ', '1005'); Gson gson = new Gson(); String json = gson.toJson(w); System.out.println('json輸出:'+json); System.out.println('-----------------------------------------'); Weather w2 = gson.fromJson(json, Weather.class); System.out.println('對象輸出:'+w2); }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

控制臺輸出:

json輸出(便于觀察,,轉(zhuǎn)成json格式排版):{ 'city': '北京', 'cityId': '10000000001', 'temp': '24', 'wd': '南風(fēng)', 'ws': '2級', 'sd': '74%', 'wse': '2', 'time': '17:45', 'isReader': '1', 'Radar': 'JC_RADAR_AZ9010_JB', 'njd': '暫無實況\t', 'qy': '1005'}-----------------------------------------對象輸出(便于觀察,轉(zhuǎn)成json格式排版,,實際toString):Weather[ city=北京, cityId=10000000001, temp=24, wd=南風(fēng), ws=2級, sd=74%, wse=2, time=17: 45, isReader=1, Radar=JC_RADAR_AZ9010_JB, njd=暫無實況, qy=1005]
  • 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
  • 31

3.現(xiàn)在來個稍微復(fù)雜一點的包含內(nèi)部類
實體如下(BoyStudent 包含Student內(nèi)部類):

public class Student { private int id; private String name; private Date birthDay; public Student(){} public Student(int id, String name, Date birthDay) { super(); this.id = id; this.name = name; this.birthDay = birthDay; } public String toString() { return 'Student [birthDay=' + birthDay + ', id=' + id + ', name=' + name + ']'; }}public class BoyStudent { private String sex; private String intertests; private Student student; @Override public String toString() { return 'Boy [sex=' + sex + ', intertests=' + intertests + ', student=' + student + ']'; } public Boy(String sex, String intertests, Student student) { super(); this.sex = sex; this.intertests = intertests; this.student = student; } public Boy() { }}
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

(Serialization)序列化,(Deserialization)反序列化操作操作如下:

public static void main(String[] args) { // TODO Auto-generated method stub Gson gson = new Gson(); Student s1 = new Student(1,'s1',new Date()); BoyStudent b1 = new BoyStudent('m', '籃球', s1); String json1 = gson.toJson(b1); System.out.println('json1:'+json1); System.out.println('---------------------------'); BoyStudent bTemp = gson.fromJson(json1, BoyStudent.class); System.out.println('bTemp:'+bTemp); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

輸出:

json1:{ 'sex': 'm', 'intertests': '籃球', 'student': { 'id': 1, 'name': 's1', 'birthDay': 'Mar 21, 2016 9:44:05 PM' }}---------------------------bTemp:BoyStudent[ sex=m, intertests=籃球, student=Student[ birthDay=MonMar2121: 44: 05CST2016, id=1, name=s1 ]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

4.在開發(fā)中那么這還是僅僅不夠的,,進(jìn)場返回來的json數(shù)據(jù)是一個集合,那么gson能否實現(xiàn)呢,?哈哈~那是必須的,,現(xiàn)在來個復(fù)雜一點的。
實體如下:

public class ResponseBoyStudent { private int errCode; private String note; private BoyStudent boyStudent; public ResponseBoyStudent(int errCode, String note, BoyStudent boyStudent) { super(); this.errCode = errCode; this.note = note; this.boyStudent = boyStudent; } @Override public String toString() { return 'ResponseBoyStudent [errCode=' + errCode + ', note=' + note + ', boyStudent=' + boyStudent + ']'; }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

(Serialization)序列化,(Deserialization)反序列化操作操作如下:

public static void main(String[] args) { // TODO Auto-generated method stub Student s1 = new Student(1, 'TFboys-1', new Date()); Student s2 = new Student(2, 'TFboys-2', new Date()); Student s3 = new Student(3, 'TFboys-3', new Date()); BoyStudent b1 = new BoyStudent('M', '唱歌', s1); BoyStudent b2 = new BoyStudent('M', '跳舞', s2); BoyStudent b3 = new BoyStudent('M', '啥都不會', s3); ResponseBoyStudent rbs1 = new ResponseBoyStudent(200, 'Ok', b1); ResponseBoyStudent rbs2 = new ResponseBoyStudent(302, '跳轉(zhuǎn)', b2); ResponseBoyStudent rbs3 = new ResponseBoyStudent(404, '資源請求失敗', null); List tfboys = new ArrayList(); tfboys.add(rbs1); tfboys.add(rbs2); tfboys.add(rbs3); Gson gson = new Gson(); String json = gson.toJson(tfboys); System.out.println(json); System.out.println('--------------------------------'); List rec_tfboys = gson.fromJson(json, new TypeToken<>>() { }.getType()); System.out.println(rec_tfboys); }
  • 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
  • 31

控制臺輸出:

[ { 'errCode': 200, 'note': 'Ok', 'boyStudent': { 'sex': 'M', 'intertests': '唱歌', 'student': { 'id': 1, 'name': 'TFboys-1', 'birthDay': 'Mar 21, 2016 10:06:54 PM' } } }, { 'errCode': 302, 'note': '跳轉(zhuǎn)', 'boyStudent': { 'sex': 'M', 'intertests': '跳舞', 'student': { 'id': 2, 'name': 'TFboys-2', 'birthDay': 'Mar 21, 2016 10:06:54 PM' } } }, { 'errCode': 404, 'note': '資源請求失敗' }]--------------------------------[ ResponseBoyStudent[ errCode=200, note=Ok, boyStudent=BoyStudent[ sex=M, intertests=唱歌, student=Student[ birthDay=MonMar2122: 06: 54CST2016, id=1, name=TFboys-1 ] ] ], ResponseBoyStudent[ errCode=302, note=跳轉(zhuǎn), boyStudent=BoyStudent[ sex=M, intertests=跳舞, student=Student[ birthDay=MonMar2122: 06: 54CST2016, id=2, name=TFboys-2 ] ] ], ResponseBoyStudent[ errCode=404, note=資源請求失敗, boyStudent=null ]]
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

看到這里有木有想哭的趕腳,,之前寫的代碼真叫尼瑪一個累~
注意:好好觀察哦,,當(dāng)404傳入為空的時候,在生成Json字符串是沒有boyStudent這個鍵值的,,同時當(dāng)反序列化為list對象時boyStudent是為空的,!
好了看到這里,會想到另外一種需求,,BoyStudent不能為空,,而且它是有默認(rèn)值的,但是返回來的卻是null,,那么在程序中像這樣rec_tfboys.get(postion).getBoyStudent().get….使用是一定會報空指針異常的,。總不至于不讓后臺傳入空值吧,?會不會有其他的解決辦法呢,?帶著問題思考,既然提到這里了,,答案一定是有的,,再次之前就說Gson吊炸天了吧!我說的沒錯吧,,廢話不多說,,直接來碼代碼吧~^_^

我對幾個實體類稍微做了一下修改,注意觀察修改了那些內(nèi)容,,注意哪些是變化的,,哪些是沒有變化的

public class Student { private int id; private String name; private Date birthDay; /////////////////////Change///////////////////////////// public Student(){ this.id = 0; this.name = '請輸入您的姓名'; this.birthDay = new Date(); } /////////////////////Change///////////////////////////// public Student(int id, String name, Date birthDay) { super(); this.id = id; this.name = name; this.birthDay = birthDay; } public String toString() { return 'Student [birthDay=' + birthDay + ', id=' + id + ', name=' + name + ']'; }}public class BoyStudent { private String sex; private String intertests; private Student student; @Override public String toString() { return 'BoyStudent [sex=' + sex + ', intertests=' + intertests + ', student=' + student + ']'; } public BoyStudent(String sex, String intertests, Student student) { super(); this.sex = sex; this.intertests = intertests; this.student = student; } /////////////////////Change///////////////////////////// public BoyStudent() { this.sex = 'M'; this.intertests = '左手右手一個慢動作'; this.student = new Student(1000, '請輸入您的姓名', new Date()); } /////////////////////Change/////////////////////////////}public class ResponseBoyStudent { private int errCode; private String note; private BoyStudent boyStudent; public ResponseBoyStudent(int errCode, String note, BoyStudent boyStudent) { super(); this.errCode = errCode; this.note = note; this.boyStudent = boyStudent; } /////////////////////Change///////////////////////////// public ResponseBoyStudent(){ boyStudent = new BoyStudent('M','睡覺',new Student()); } ////////////////////////////////////////////////// @Override public String toString() { return 'ResponseBoyStudent [errCode=' + errCode + ', note=' + note + ', boyStudent=' + boyStudent + ']'; }}
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

我將用斜線表示出來改變的部分,一次是對其無參構(gòu)造函數(shù)進(jìn)行了修改,,我們來看看效果是否如我們所想那樣,。
序列化反序列化操作操作代碼未進(jìn)行修改,。
控制臺輸出:

[ { 'errCode': 200, 'note': 'Ok', 'boyStudent': { 'sex': 'M', 'intertests': '唱歌', 'student': { 'id': 1, 'name': 'TFboys-1', 'birthDay': 'Mar 21, 2016 10:41:43 PM' } } }, { 'errCode': 302, 'note': '跳轉(zhuǎn)', 'boyStudent': { 'sex': 'M', 'intertests': '跳舞', 'student': { 'id': 2, 'name': 'TFboys-2', 'birthDay': 'Mar 21, 2016 10:41:43 PM' } } }, { 'errCode': 404, 'note': '資源請求失敗' }]--------------------------------[ ResponseBoyStudent[ errCode=200, note=Ok, boyStudent=BoyStudent[ sex=M, intertests=唱歌, student=Student[ birthDay=MonMar2122: 41: 43CST2016, id=1, name=TFboys-1 ] ] ], ResponseBoyStudent[ errCode=302, note=跳轉(zhuǎn), boyStudent=BoyStudent[ sex=M, intertests=跳舞, student=Student[ birthDay=MonMar2122: 41: 43CST2016, id=2, name=TFboys-2 ] ] ], ResponseBoyStudent[ errCode=404, note=資源請求失敗, boyStudent=BoyStudent[ sex=M, intertests=睡覺, student=Student[ birthDay=MonMar2122: 41: 43CST2016, id=0, name=請輸入您的姓名 ] ] ]]
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

**可以發(fā)現(xiàn)json字符串未發(fā)生改變,但是反序列化的list的內(nèi)容都是部位空的,,可以推出,,json字符串中缺少鍵值的時候仍會去“調(diào)用”默認(rèn)構(gòu)造函數(shù)。

5.既然能讓空的json反序列話為不為null的實例,,那能不能返回null?不買關(guān)子了,是有的^_^.
代碼很簡單:

public static void main(String[] args) { // TODO Auto-generated method stub Gson gson = new GsonBuilder().serializeNulls().create(); String json = gson.toJson(null); System.out.println(json); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

控制臺輸出:

null
  • 1

為什么非要去Builder一個Gson實例呢,,難道直接new 出來的不可以嗎,?親測確實可以,但是這兩種方式有什么區(qū)別呢,?

public static void main(String[] args) { // TODO Auto-generated method stub Gson gson = new Gson(); String json = gson.toJson(null); System.out.println(json); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

控制臺輸出:

null
  • 1

好,,那我么就來觀察它的區(qū)別吧!
直接上代碼了
實體如下:

public class Foo { private final String s; private final int i; public Foo() { this(null, 5); } public Foo(String s, int i) { this.s = s; this.i = i; } @Override public String toString() { return 'Foo [s=' + s + ', i=' + i + ']'; }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

(Serialization)序列化,(Deserialization)反序列化操作操作如下:
先看直接new出來的Gson

public static void main(String[] args) { // TODO Auto-generated method stub Gson gson = new Gson(); Foo foo = new Foo(); String json = gson.toJson(foo); System.out.println(json); Foo f = gson.fromJson(json, Foo.class); System.out.println('f:'+f); json = gson.toJson(null); System.out.println(json); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

控制臺輸出:

{'i':5}f:Foo [s=null, i=5]null
  • 1
  • 2
  • 3

在biu~出來的效果

public static void main(String[] args) { // TODO Auto-generated method stub Gson gson = new GsonBuilder().serializeNulls().create(); Foo foo = new Foo(); String json = gson.toJson(foo); System.out.println(json); Foo f = gson.fromJson(json, Foo.class); System.out.println('f:'+f); json = gson.toJson(null); System.out.println(json); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

控制臺輸出:

{'s':null,'i':5}f:Foo [s=null, i=5]null
  • 1
  • 2
  • 3

看出二者的區(qū)別了吧,,當(dāng)使用biu~方式時屬性為空的時候輸出來的json字符串是有鍵值key的,,而直接new出來的就沒有。

非常感謝看到這里的朋友,,最后送點一個非常實用的工具給你
這個工具叫做json實體轉(zhuǎn)換器,,用法很簡單,我直接貼圖就好了,。
這里寫圖片描述
確認(rèn)提交之后將自動生成相應(yīng)的實體
這里寫圖片描述
看看生成的實體張什么樣子吧

工具下載地址:http://download.csdn.net/detail/u013922681/9468715

package domain;import java.lang.reflect.Field;import java.io.Serializable;import java.util.List;public class JavaName implements Serializable { public String note; public int errCode; public BoyStudent boyStudent; public class BoyStudent implements Serializable { public String sex; public String intertests; public Student student; public class Student implements Serializable { public String birthDay; public String name; public int id; public void setBirthDay(String birthDay) { this.birthDay = birthDay; } public String getBirthDay() { return this.birthDay; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setId(int id) { this.id = id; } public int getId() { return this.id; } public String toString() { String s = ''; Field[] arr = this.getClass().getFields(); for (Field f : getClass().getFields()) { try { s += f.getName() + '=' + f.get(this) + '\n,'; } catch (Exception e) { } } return getClass().getSimpleName() + '[' + (arr.length == 0 ? s : s.substring(0, s.length() - 1)) + ']'; } } public void setSex(String sex) { this.sex = sex; } public String getSex() { return this.sex; } public void setIntertests(String intertests) { this.intertests = intertests; } public String getIntertests() { return this.intertests; } public String toString() { String s = ''; Field[] arr = this.getClass().getFields(); for (Field f : getClass().getFields()) { try { s += f.getName() + '=' + f.get(this) + '\n,'; } catch (Exception e) { } } return getClass().getSimpleName() + '[' + (arr.length == 0 ? s : s.substring(0, s.length() - 1)) + ']'; } } public void setNote(String note) { this.note = note; } public String getNote() { return this.note; } public void setErrCode(int errCode) { this.errCode = errCode; } public int getErrCode() { return this.errCode; } public String toString() { String s = ''; Field[] arr = this.getClass().getFields(); for (Field f : getClass().getFields()) { try { s += f.getName() + '=' + f.get(this) + '\n,'; } catch (Exception e) { } } return getClass().getSimpleName() + '[' + (arr.length == 0 ? s : s.substring(0, s.length() - 1)) + ']'; }}
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121

好了,,第一次寫這么認(rèn)真~覺得可以的話點個贊!
對了,,能看官網(wǎng)的demo就看官網(wǎng)的吧,!
鏈接如下:https://sites.google.com/site/gson/gson-user-guide#TOC-Gson-Users

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點,。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多