Created
October 28, 2019 02:58
-
-
Save CodeK1988/de01f5a24ef288ac81c1310c529df27a to your computer and use it in GitHub Desktop.
Gson使用技巧
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1.正常使用大家都知道 fromJson等 | |
| 2.将json字符串中某个字段转换成实体类代码如下 | |
| if (data.has("pop")) { | |
| val jsonElement = Gson().fromJson<JsonElement>(json, JsonElement::class.java) | |
| val pop = jsonElement.asJsonObject.getAsJsonObject("data").getAsJsonObject("pop") | |
| val bean = Gson().fromJson<Bean>(pop, Bean::class.java) | |
| } | |
| 3.如果接口给的数据不规范 同一个字段有不同的类型处理借助 | |
| JsonDeserializer | |
| val fromJson = Gson().fromJson(jsonElement, Bean::class.java) | |
| if fromJson.xx== | |
| else fromJson.xx== | |
| return fromJson | |
| 更多参考:https://github.com/google/gson/blob/master/UserGuide.md |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment