前言
在實際開發(fā)項目中,服務(wù)器經(jīng)常會用空字符串 “” 作為返回結(jié)果表示空值 ,但這在Gson當(dāng)中就會遇到問題,如果這項數(shù)據(jù)的類型不是字符串,Gson解析就會報錯
Json異常情況
先來看一個后臺返回的json
正常情況下json:
1
2
3
4
5
6
7
8
|
{ "code" : 0 , "msg" : "ok" , "data" :{ "id" : 5638 , "newsId" : 5638 } } |
data部分對應(yīng)的實體類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class JsonBean { private int id; private int newsId; public int getId() { return id; } public void setId( int id) { this .id = id; } public int getNewsId() { return newsId; } public void setNewsId( int newsId) { this .newsId = newsId; } } |
異常情況json(后臺數(shù)據(jù)庫newsId字段未查詢到對應(yīng)數(shù)據(jù)):
1
2
3
4
5
6
7
8
|
{ "code" : 0 , "msg" : "ok" , "data" :{ "id" : 5638 , "newsId" : "" } } |
這樣Gson在解析時就會拋出解析錯誤的異常,app崩潰,原因是無法將""轉(zhuǎn)化為int
json異常的處理
我們期望在后臺返回的json異常時,也能解析成功,空值對應(yīng)的轉(zhuǎn)換為默認(rèn)值,如:newsId=0;
這里排除掉后臺開發(fā)人員輸出時給你做矯正,還是得靠自己啊---
我們寫一個針對int值的類型轉(zhuǎn)換器,需要實現(xiàn)Gson的 JsonSerializer<T>
接口和 JsonDeserializer<T>
,即序列化和反序列化接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class IntegerDefault0Adapter implements JsonSerializer<Integer>, JsonDeserializer<Integer> { @Override public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (json.getAsString().equals( "" ) || json.getAsString().equals( "null" )) { //定義為int類型,如果后臺返回""或者null,則返回0 return 0 ; } } catch (Exception ignore) { } try { return json.getAsInt(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } @Override public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); } } |
同理Long及Double類型
double=>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class DoubleDefault0Adapter implements JsonSerializer<Double>, JsonDeserializer<Double> { @Override public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (json.getAsString().equals( "" ) || json.getAsString().equals( "null" )) { //定義為double類型,如果后臺返回""或者null,則返回0.00 return 0.00 ; } } catch (Exception ignore) { } try { return json.getAsDouble(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } @Override public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); } } |
long=>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class LongDefault0Adapter implements JsonSerializer<Long>, JsonDeserializer<Long> { @Override public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (json.getAsString().equals( "" ) || json.getAsString().equals( "null" )) { //定義為long類型,如果后臺返回""或者null,則返回0 return 0l; } } catch (Exception ignore) { } try { return json.getAsLong(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } @Override public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); } } |
所以使用是這樣的:
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
|
return new Retrofit.Builder() .client(okHttpClient) //設(shè)置網(wǎng)絡(luò)訪問框架 .addConverterFactory(GsonConverterFactory.create(buildGson())) //添加json轉(zhuǎn)換框架 .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) //讓Retrofit支持RxJava .baseUrl(baseUrl) .build(); /** * 增加后臺返回""和"null"的處理 * 1.int=>0 * 2.double=>0.00 * 3.long=>0L * * @return */ public static Gson buildGson() { if (gson == null ) { gson = new GsonBuilder() .registerTypeAdapter(Integer. class , new IntegerDefault0Adapter()) .registerTypeAdapter( int . class , new IntegerDefault0Adapter()) .registerTypeAdapter(Double. class , new DoubleDefault0Adapter()) .registerTypeAdapter( double . class , new DoubleDefault0Adapter()) .registerTypeAdapter(Long. class , new LongDefault0Adapter()) .registerTypeAdapter( long . class , new LongDefault0Adapter()) .create(); } return gson; } |
再也不會因為后臺json字段為空的情況崩潰了
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能有所幫助,如果有疑問大家可以留言交流。