Gson [MavenDependency]といくつかの簡単な例
Gson
Mavne依存
com.google.code.gson gson 2.2.4
Gson jarパッケージのダウンロードアドレス:http://repo1.maven.org/maven2/com/google/code/gson/gson/2.8.0/
Gsonのインスタンス化:
1:グソン gson = 新着 グソン()
2:GsonBuilderを介して複数のオプションを構成できます
- gson = new GsonBuilder()
- .setLenient()// jsonルーズ
- .enableComplexMapKeySerialization()//マップのキーを複雑なオブジェクトの形式としてサポートします
- .serializeNulls()//スマートヌル
- .setPrettyPrinting()//チューニングフォーマット
- .disableHtmlEscaping()//デフォルトはHTMLをエスケープするためのGSONです
- .create()
Gsonの基本的な使用法:
Json文字列を変換するJavaBean
Gsonは提供しています 公衆 String toJson(Object src)メソッドは、オブジェクトをjsonStrに変換します
- パッケージcom.xuan.gson
- com.google.gson.Gsonをインポートします
- / **
- * @author xuanyouwu
- * @email root @ xxxxx
- * @time 2016-05-18 10:39
- * /
- パブリッククラスGsonTest1 {
- パブリック静的クラスStudent {
- プライベート文字列名
- プライベートint年齢
- //セッターゲッターが等しいを省略します
- }
- private static void log(String msg){
- System.out.println(msg)
- }
- public static void main(String [] args)は例外をスローします{
- Gson gson =新しいGson()
- 学生学生=新しい学生()
- student.setName( 'xuanyouwu')
- student.setAge(26)
- 文字列jsonStr = gson.toJson(学生)
- log( '----> javabean convert jsonStr:' + jsonStr)
- }
- }
演算結果:
----> javabean convert jsonStr:{'name': 'xuanyouwu'、 'age':26}
リストマップからJson文字列
- パッケージcom.xuan.gson
- com.google.gson.Gsonをインポートします
- java.util.Arraysをインポートします
- java.util.HashMapをインポートします
- java.util.Listをインポートします
- java.util.Mapをインポートします
- / **
- * @author xuanyouwu
- * @email root @ xxxxx
- * @time 2016-05-18 10:39
- * /
- パブリッククラスGsonTest1 {
- private static void log(String msg){
- System.out.println(msg)
- }
- public static void main(String [] args)は例外をスローします{
- Gson gson =新しいGson()
- リストリスト= Arrays.asList( '1'、 'a'、 '3'、 'rt'、 '5')
- log( '----> list convert jsonStr:' + gson.toJson(list))
- マップコンテンツ= new HashMap()
- content.put( 'name'、 'xuanyouwu')
- content.put( 'age'、 '26')
- log( '----> map convert jsonStr:' + gson.toJson(content))
- }
- }
- 演算結果:
- ----> list convert jsonStr:['1'、 'a'、 '3'、 'rt'、 '5']
- ----> map convert jsonStr:{'name': 'xuanyouwu'、 'age': '26'}
JavaBeanへのJson文字列
- 文字列studentJsonStr = '{' name ':' xuanyouwu '、' age ':26}'
- 学生student1 = gson.fromJson(studentJsonStr、Student.class)
- log( '-------> json convert JavaBean:' + student1)
結果:
-------> json convert JavaBean:Student {name = 'xuanyouwu'、age = 26}
リストへのJson文字列
- 文字列listJsonStr = '[' 1 '、' a '、' 3 '、' rt '、' 5 ']'
- タイプtype = new TypeToken
(){ - } .getType()
- ArrayList sList = gson.fromJson(listJsonStr、type)
- log( '-------> json convert List:' + sList)
- 演算結果:
- -------> json convert List:[1、a、3、rt、5]
Gson-packaged type system
[java] view plain copy
- Gson has a very interesting abstract base class JsonElement, his inheritance system:
[java] view plain copy
[java] view plain copy
- JsonObject is equivalent to org's JSONObject, and JsonArray is similar.
[java] view plain copy
- JsonNull is actually a null field
[java] view plain copy
- JsonNull jsonNull=new JsonNull()//The construction method is outdated, recommend INSTANCE
[java] view plain copy
- JsonNull jsonNull=JsonNull.INSTANCE
- log('-----'+jsonNull)
[java] view plain copy
- operation result:
[java] view plain copy
- -----null
JsonPrimitive is very interesting. We know that if json is converted to a string, it may contain quotation marks, but with JsonPrimative we can get the escaped string, see the example:
[java] view plain copy
- package com.xuan.gson
- import com.google.gson.JsonNull
- import com.google.gson.JsonPrimitive
- /**
- * @author xuanyouwu
- * @email root@xxxxx
- * @time 2016-05-18 11:20
- */
- public class GsonTest2 {
- private static void log(String msg) {
- System.out.println(msg)
- }
- public static void main(String[] args) throws Exception {
- String studentJsonStr='{'name':'xuanyouwu','age':26}'
- log('------>studentJsonStr:'+studentJsonStr)
- JsonPrimitive jsonPrimitive=new JsonPrimitive(studentJsonStr)
- log('------>jsonPrimitive:'+jsonPrimitive)
- log('------>jsonPrimitive:'+jsonPrimitive.toString())
- log('------>jsonPrimitive:'+jsonPrimitive.getAsString())
- JsonPrimitive jsonPrimitive2=new JsonPrimitive('this is String')
- log('------>jsonPrimitive2:'+jsonPrimitive2)
- log('------>jsonPrimitive2:'+jsonPrimitive2.toString())
- log('------>jsonPrimitive2:'+jsonPrimitive2.getAsString())
- }
- }
operation result:
------>studentJsonStr:{'name':'xuanyouwu','age':26}
------>jsonPrimitive:'{'name':'xuanyouwu','age':26}'
------>jsonPrimitive:'{'name':'xuanyouwu','age':26}'
------>jsonPrimitive:{'name':'xuanyouwu','age':26}
------>jsonPrimitive2:'this is String'
------>jsonPrimitive2:'this is String'
------>jsonPrimitive2:this is String
Create a JsonObject
Add fields to jsonObject via addPropert(key,value) Similar to hashMap
[java] view plain copy
- JsonObject jsonObject=new JsonObject()
- jsonObject.addProperty('name','xuanyouwu')
- jsonObject.addProperty('age',26)
- log('------>create jsonObject:'+jsonObject)
operation result:
------>create jsonObject:{'name':'xuanyouwu','age':26}
Create a JsonArray
[java] view plain copy
- JsonArray jsonElements=new JsonArray()
- jsonElements.add('a')
- jsonElements.add('b')
- jsonElements.add('c')
- jsonElements.add('d')
- log('------>create jsonArray:'+jsonElements)
operation result:
------>create jsonArray:['a','b','c','d']
JsonObject nested array or nested JsonArray
Add an array of fields to jsonObject via JsonObject's add(key, JsonElement)
[java] view plain copy
- JsonObject jsonObject2=new JsonObject()
- jsonObject2.addProperty('name','xuanyouwu')
- jsonObject2.addProperty('age',26)
- JsonArray jsonElements2=new JsonArray()
- jsonElements2.add('riding')
- jsonElements2.add('playing game')
- jsonElements2.add('watching TV')
- jsonObject2.add('hobby',jsonElements2)
- log('------>create jsonObject inner JsonArray:'+jsonObject2)
operation result:
------>create jsonObject inner JsonArray:{'name':'xuanyouwu','age':26,'hobby':['cycling', 'playing games', 'watching TV']}
Gson annotation
There are five types of annotations in Gson
Rename annotation: SerializedName
Role: conversion keyword key, json converted to JavaBean, json field key must be the same as the field name of the class we declare, when the server returns the keyword how to do, such as the key is new switch, we are in the declaration class When you can't write such a field, you may want to change the server side, he may have to change database , but I tell you that most of the server is not willing to change his json, it is very selfish! This time the renaming annotations come in handy. The second scenario: the json key returned by the server is simply ugly, or Too long, you want to simplify, my_parent_name, can be simplified to mpn
Example:
[java] view plain copy
- package com.xuan.gson
- import com.google.gson.Gson
- import com.google.gson.annotations.SerializedName
- /**
- * @author xuanyouwu
- * @email root@xxxxx
- * @time 2016-05-18 11:20
- */
- public class GsonTest3 {
- private static void log(String msg) {
- System.out.println(msg)
- }
- public static class User {
- public String name
- public int age
- @SerializedName('new')
- public int isnew
- @Override
- public String toString() {
- return 'User{' +
- 'name='' + name + ''' +
- ', age=' + age +
- ', isnew=' + isnew +
- '}'
- }
- }
- public static void main(String[] args) throws Exception {
- String jsonFromServer = '{ ' +
- ' 'age': 26, ' +
- ' 'name': 'zhangsan', ' +
- ' 'new': 1 ' +
- '}'
- Gson gson = new Gson()
- User user = gson.fromJson(jsonFromServer, User.class)
- log('------>user:' + user)
- }
- }
Run result: ------>user:User{name='zhangsan', age=26, isnew=1}
[java] view plain copy
- package com.xuan.gson
- import com.google.gson.Gson
- import com.google.gson.annotations.SerializedName
- /**
- * @author xuanyouwu
- * @email root@xxxxx
- * @time 2016-05-18 11:20
- */
- public class GsonTest3 {
- private static void log(String msg) {
- System.out.println(msg)
- }
- public static class User2 {
- public String name
- public int age
- @SerializedName('my_parent_name')
- public String pn
- @Override
- public String toString() {
- return 'User2{' +
- 'name='' + name + ''' +
- ', age=' + age +
- ', pn='' + pn + ''' +
- '}'
- }
- }
- public static void main(String[] args) throws Exception {
- String jsonFromServer2='{ ' +
- ' 'age': 26, ' +
- ' 'my_parent_name': 'zhangsanf', ' +
- ' 'name': 'zhangsan' ' +
- '}'
- Gson gson2 = new Gson()
- User2 user2 = gson2.fromJson(jsonFromServer2, User2.class)
- log('------>user2:' + user2)
- }
- }
Run results: ------>user2:User2{name='zhangsan', age=26, pn='zhangsanf'}
Role 2: Combine alternate to provide multiple alternate field keys for parsing, @SerializedName(value = 'desc' ,alternate = { 'other' , 'note' }) If there is another in json, it will be parsed into desc. If there is a note, it will be parsed into desc. Note that the value in 1:value cannot appear in the alternate. Note that the alternative field of 2:alternate will be replaced by the previous one.
Example:
[java] view plain copy
- package com.xuan.gson
- import com.google.gson.Gson
- import com.google.gson.annotations.SerializedName
- /**
- * @author xuanyouwu
- * @email root@xxxxx
- * @time 2016-05-18 11:20
- */
- public class GsonTest4 {
- private static void log(String msg) {
- System.out.println(msg)
- }
- public static class User {
- public String name
- public int age
- @SerializedName(value = 'desc',alternate = {'other','note'})
- public String desc
- @Override
- public String toString() {
- return 'User{' +
- 'name='' + name + ''' +
- ', age=' + age +
- ', desc='' + desc + ''' +
- '}'
- }
- }
- public static void main(String[] args) throws Exception {
- String jsonFromServer = '{ ' +
- ' 'age': 26, ' +
- ' 'other': 'Chengdu people', ' +
- ' 'name': 'zhangsan' ' +
- '}'
- Gson gson = new Gson()
- User user = gson.fromJson(jsonFromServer, User.class)
- log('------>user:' + user)
- String jsonFromServer2 = '{ ' +
- ' 'age': 26, ' +
- ' 'note': 'Chengdu people', ' +
- ' 'name': 'zhangsan' ' +
- '}'
- User user2 = gson.fromJson(jsonFromServer2, User.class)
- log('------>user:' + user2)
- //Include desc and note note after desc
- String jsonFromServer3='{ ' +
- ' 'age': 26, ' +
- ' 'desc': 'desc Chengdu people', ' +
- ' 'name': 'zhangsan', ' +
- ' 'note': 'note Chengdu people' ' +
- '}'
- User user3 = gson.fromJson(jsonFromServer3, User.class)
- log('------>user:' + user3)
- //Include desc and note note before desc
- String jsonFromServer4='{ ' +
- ' 'age': 26, ' +
- ' 'note': 'note Chengdu people', ' +
- ' 'name': 'zhangsan', ' +
- ' 'desc': 'desc Chengdu people' ' +
- '}'
- User user4 = gson.fromJson(jsonFromServer4, User.class)
- log('------>user:' + user4)
- }
- }
operation result:
------>user:User{name='zhangsan', age=26, desc='Chengdu people'}
------>user:User{name='zhangsan', age=26, desc='Chengdu people'}
------>user:User{name='zhangsan', age=26, desc='note Chengdu people'}
------>user:User{name='zhangsan', age=26, desc='desc Chengdu people'}
Gson @Expose filter annotation
Source code: Default can be serialized or deserialized
[java] view plain copy
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.FIELD)
- public @interface Expose {
- public boolean serialize() default true
- public boolean deserialize() default true
- }
[java] view plain copy
can exclude fields that don't need to be serialized and need to be used with GsonBuilder
[java] view plain copy
Gson gson = new GsonBuilder ()
- .excludeFieldsWithoutExposeAnnotation()
- .create()
Fields that do not add @Expose annotations will not be parsed:
Divided into the following situations:
1: Do not add @Expose annotation equivalent to @Expose(deserialize = false ,serialize = false ) do not do any analysis
2:@Expose(deserialize = true ,serialize = false ) only parsing, that is, deserialization can be, serialization can not
3:@Expose(deserialize = false ,serialize = true Serialization can, deserialization is not possible
4:@Expose(deserialize = true ,serialize = true ) can be serialized or deserialized
Example: These four cases will be demonstrated separately
Do not add @Expose annotations : equivalent to @Expose(deserialize = false ,serialize = false )
- パッケージcom.xuan.gson
- com.google.gson.Gsonをインポートします
- com.google.gson.GsonBuilderをインポートします
- / **
- * @author xuanyouwu
- * @email root @ xxxxx
- * @time 2016-05-18 11:20
- * /
- パブリッククラスGsonTest5 {
- private static void log(String msg){
- System.out.println(msg)
- }
- public static class User {
- パブリック文字列名
- @オーバーライド
- public String toString(){
- 'User {' +を返します
- 'name =' '+ name +' '' +
- '}'
- }
- }
- public static void main(String [] args)は例外をスローします{
- 文字列jsonFromServer = '{' name ':' zhangsan '}'
- Gson gson = new GsonBuilder()
- .excludeFieldsWithoutExposeAnnotation()
- .create()
- ユーザーuser = gson.fromJson(jsonFromServer、User.class)
- Log( '------>逆シリアル化:' +ユーザー)
- ユーザーuser1 = new User()
- user1.name = 'zhangsan2'
- 文字列userStr = gson.toJson(user1)
- Log( '------>シリアル化:' + userStr)
- }
- }
operation result:
------>Deserialization: User{name='null'} ------>Serialization:{}
Add @Expose annotation
- パッケージcom.xuan.gson
- com.google.gson.Gsonをインポートします
- com.google.gson.GsonBuilderをインポートします
- インポートcom.google.gson.annotations.Expose
- / **
- * @author xuanyouwu
- * @email root @ xxxxx
- * @time 2016-05-18 11:20
- * /
- パブリッククラスGsonTest5 {
- private static void log(String msg){
- System.out.println(msg)
- }
- public static class User {
- @Expose //は@Expose(deserialize = true、serialize = true)と同等です
- パブリック文字列名
- @オーバーライド
- public String toString(){
- 'User {' +を返します
- 'name =' '+ name +' '' +
- '}'
- }
- }
- public static void main(String [] args)は例外をスローします{
- 文字列jsonFromServer = '{' name ':' zhangsan '}'
- Gson gson = new GsonBuilder()
- .excludeFieldsWithoutExposeAnnotation()
- .create()
- ユーザーuser = gson.fromJson(jsonFromServer、User.class)
- Log( '------>逆シリアル化:' +ユーザー)
- ユーザーuser1 = new User()
- user1.name = 'zhangsan2'
- 文字列userStr = gson.toJson(user1)
- Log( '------>シリアル化:' + userStr)
- }
- }
演算結果:
------>逆シリアル化:User {name = 'zhangsan'}
------>シリアル化:{'name': 'zhangsan2'}
@Exposeアノテーションはシリアル化のみ
- パッケージcom.xuan.gson
- com.google.gson.Gsonをインポートします
- com.google.gson.GsonBuilderをインポートします
- インポートcom.google.gson.annotations.Expose
- / **
- * @author xuanyouwu
- * @email root @ xxxxx
- * @time 2016-05-18 11:20
- * /
- パブリッククラスGsonTest5 {
- private static void log(String msg){
- System.out.println(msg)
- }
- public static class User {
- @Expose(deserialize = false、serialize = true)
- パブリック文字列名
- @オーバーライド
- public String toString(){
- 'User {' +を返します
- 'name =' '+ name +' '' +
- '}'
- }
- }
- public static void main(String [] args)は例外をスローします{
- 文字列jsonFromServer = '{' name ':' zhangsan '}'
- Gson gson = new GsonBuilder()
- .excludeFieldsWithoutExposeAnnotation()
- .create()
- ユーザーuser = gson.fromJson(jsonFromServer、User.class)
- Log( '------>逆シリアル化:' +ユーザー)
- ユーザーuser1 = new User()
- user1.name = 'zhangsan2'
- 文字列userStr = gson.toJson(user1)
- Log( '------>シリアル化:' + userStr)
- }
- }
演算結果:
------>逆シリアル化:User {name = 'null'} ------>シリアル化:{'name': 'zhangsan2'}
@Exposeは逆シリアル化のみ
- パッケージcom.xuan.gson
- com.google.gson.Gsonをインポートします
- com.google.gson.GsonBuilderをインポートします
- インポートcom.google.gson.annotations.Expose
- / **
- * @author xuanyouwu
- * @email root @ xxxxx
- * @time 2016-05-18 11:20
- * /
- パブリッククラスGsonTest5 {
- private static void log(String msg){
- System.out.println(msg)
- }
- public static class User {
- @Expose(deserialize = true、serialize = false)
- パブリック文字列名
- @オーバーライド
- public String toString(){
- 'User {' +を返します
- 'name =' '+ name +' '' +
- '}'
- }
- }
- public static void main(String [] args)は例外をスローします{
- 文字列jsonFromServer = '{' name ':' zhangsan '}'
- Gson gson = new GsonBuilder()
- .excludeFieldsWithoutExposeAnnotation()
- .create()
- ユーザーuser = gson.fromJson(jsonFromServer、User.class)
- Log( '------>逆シリアル化:' +ユーザー)
- ユーザーuser1 = new User()
- user1.name = 'zhangsan2'
- 文字列userStr = gson.toJson(user1)
- Log( '------>シリアル化:' + userStr)
- }
- }
演算結果:
------>逆シリアル化:User {name = 'zhangsan'}
------>シリアル化:{}
@Since(float v)アノテーション バージョン管理
GsonBuilder.setVersion(n)と組み合わせると、n> = vのときにシリアル化されます。
- パッケージcom.xuan.gson
- com.google.gson.Gsonをインポートします
- com.google.gson.GsonBuilderをインポートします
- com.google.gson.annotations.Sinceをインポートします
- / **
- * @author xuanyouwu
- * @email root @ xxxxx
- * @time 2016-05-18 11:20
- * /
- パブリッククラスGsonTest6 {
- private static void log(String msg){
- System.out.println(msg)
- }
- public static class User {
- @Since(2)
- パブリック文字列名
- @オーバーライド
- public String toString(){
- 'User {' +を返します
- 'name =' '+ name +' '' +
- '}'
- }
- }
- public static void main(String [] args)は例外をスローします{
- 文字列jsonFromServer = '{' name ':' zhangsan '}'
- Gson gson = new GsonBuilder()
- .setVersion(1)//バージョン1
- .create()
- ユーザーuser1 = gson.fromJson(jsonFromServer、User.class)
- Log( '------> Deserialization v = 1:' + user1)
- ユーザーuser1_1 = new User()
- user1_1.name = 'zhangsan2'
- 文字列userStr = gson.toJson(user1_1)
- Log( '------> serialization v = 1:' + userStr)
- Gson gson2 =新しいGsonBuilder()
- .setVersion(2)//バージョン2です
- .create()
- ユーザーuser2 = gson2.fromJson(jsonFromServer、User.class)
- Log( '------>逆シリアル化v = 2:' + user2)
- ユーザーuser2_1 = new User()
- user2_1.name = 'zhangsan2'
- 文字列userStr2_1 = gson2.toJson(user2_1)
- Log( '------> serialization v = 2:' + userStr2_1)
- Gson gson3 =新しいGsonBuilder()
- .setVersion(3)//バージョンは3です
- .create()
- ユーザーuser3 = gson3.fromJson(jsonFromServer、User.class)
- Log( '------> Deserialization v = 3:' + user3)
- ユーザーuser3_1 = new User()
- user3_1.name = 'zhangsan2'
- 文字列userStr3_1 = gson3.toJson(user3_1)
- Log( '------> serialization v = 3:' + userStr3_1)
- }
- }
演算結果:
------>逆シリアル化v = 1:User {name = 'null'}
------>シリアル化v = 1:{}
------>逆シリアル化v = 2:User {name = 'zhangsan'}
------>シリアル化v = 2:{'name': 'zhangsan2'}
------>逆シリアル化v = 3:User {name = 'zhangsan'}
------>シリアル化v = 3:{'name': 'zhangsan2'}
@Util(float v)アノテーションバージョン管理
gsonのsetVersion(n)nのときに解析されます ------>逆シリアル化v = 1:User {name = 'zhangsan'} Gsonの高度な使用法 retrofit2.0のソースコードを見た学生は、使用法の中にGsonConverterFactoryがあることを知っていると思います。ここにソースコードがあります。 TypeAdapterを使用する方が効率的だと言われていますが、比較していません テスト 一時リリース後、TypeAdapterとは何ですか? ソースコードの注釈は変換します Java JSONとの間のオブジェクトは、オブジェクトjson間の変換です。 Tジェネリッククラスのシリアル化と逆シリアル化を置き換えるロジック。 ソースコードから、2.1バージョンの前後の使用法を確認できます。 2.1バージョンより前では、アダプターをカスタマイズできます。 使用する バージョン2.1の直後にジェネリックを挿入することをお勧めします。 2:JsonReaderを使用する 3:カスタムTypeAdapter 4:アノテーションJsonAdapterを使用すると、カスタムアダプターでもあります 1.2フレームワークによってクラスとして分類され、基本的なjson大判仕様、キーと値のペアが標準ではない、複数引用符など、エラーなしで解析を停止しますが、関数は比較的弱いです バグを解決できます 3、4は1つのカテゴリに分類され、すべてカスタムアダプタに属しますが、3はgsonにバインドされ、4は注釈とフィールドバインディングを使用します 例: 演算結果: -------> jsonFromServer:{ 年齢は空の文字列であることがわかりますが、全体的な解析プロセスには影響しません。これはクライアントにとって非常に使いやすいものです。 注釈方法に基づくと、上記のアプローチは全体論的である傾向があり、注釈の方法はフィールドである傾向があります 演算結果: [java] view plain copy [java] view plain copy [java] view plain copy 実際、上記の保険の方法は非常に安全ですが、維持する場所がたくさんあり、コードの量が多いため、別の方法を紹介します:JsonSerializerとJsonDeserializerの関係の一方的な処理、タイプをグローバルに登録できます処理:例を参照してください: 演算結果: -------> jsonFromServer:{ これは、例外の解析を回避するためのグローバルな逆シリアル化ツールを定義します。 保存して、学習に使用してください。そうでない場合は、私を訂正してください。 転載: https://blog.csdn.net/sinat_34093604/article/details/53158059 ブロガー:餌をやる牛
結果:
------>シリアル化v = 1:{'name': 'zhangsan2'}
------>逆シリアル化v = 2:User {name = 'null'}
------>シリアル化v = 2:{}
------>逆シリアル化v = 3:User {name = 'null'}
------>シリアル化v = 3:{}
{@code
Example: Using TypeAdapter to serialize and deserialize
Gson's fault tolerance mechanism:
Why is it fault-tolerant, and declare the int type of age in the javaBean. What if the server returns '' an empty string? collapse?
If the json format is not standardized, such as {name=zhangsan,age:26,hobby=null} found that it is not a normal key value
Fault Tolerance Implementation 1:
1: How to create Gson
JsonReader jsonReader = gson.newJsonReader(value.charStream())
'年齢': ''、
'名前': '張山'
}
------>デフォルトのGson解析例外:com.google.gson.JsonSyntaxException:java.lang.NumberFormatException:空の文字列
------>カスタムアダプタの解析:User {name = 'zhangsan'、age = 0}
------>カスタムアダプタの解析2:User {name = 'zhangsan'、age = 0}
has to vomit, like this server-side error, it should be completely due to the unreasonable return of the server json,
パッケージcom.xuan.gson
'年齢': ''、
'名前': '張山'
}
------> JsonDeserializer解決策:User {name = 'zhangsan'、age = 0}
------>デフォルトのgson解析例外:com.google.gson.JsonSyntaxException:java.lang.NumberFormatException:空の文字列