当前位置:网站首页>Android:kotlin中Gson与JSON的泛型映射解析
Android:kotlin中Gson与JSON的泛型映射解析
2022-06-25 10:25:00 【奔跑的蜗牛、Z】
背景:
在Android中,kotlin的使用,可以极大美化代码和带来代码上的简洁,但是kotlin之前,android的主要语言是Java。
在android中,解析数据是比较常见的手法。JavaBean,GSON,JSONOBJECT,这三大员是合作伙伴,但是在kotlin中依然支持这种写法,但是kotlin的data GSON,JSONOBJECT,与Java解析有所不同
Java:
People{
private String name="";
private String sex="";
public void setName(String name)
{
this.name=name
}
public void setSex(String sex){
this.sex=sex;
}
public String getName(){
return name;
}
public String getSex()
{
return sex;
}
}
Json:{"name":"zhangshan","sex":"男"}
解析如下
People people=new Gson().fromJson(json,People.class);
这是Java的解析。
kotlin:
kotlin的解析与Java不同,JavaBean在kotlin中叫数据data
data class People(var name:String="",var sex:String="");
或者直接定义一个class类也行
class People{
var name:String=""
var sex:String=""
}
解析GSON:
我们在做解析最好能做到通用,所以在做解析工具有点不同,如下
object GsonUtil {
public inline fun <reified T> getGsonObject(json: String): T? {
val result = Gson().fromJson<T>(json)
return result
}
//type 泛型扩展
inline fun <reified T> Gson.fromJson(json: String) = this.fromJson<T>(json, object : TypeToken<T>() {}.type)
}GSON在kotlin的最大泛型处理在于type部分,所以我们在内部通过扩展函数,对Gson.fromJson进行扩展,这样我们就可以直接使用方法参数泛型解析。
这里面不同于Java的函数泛型,kotlin支持方法泛型。
总结:
不同在于bean的定义,kotlin中叫data class
边栏推荐
- OpenCV学习(一)---环境搭建
- 【文件包含漏洞-03】文件包含漏洞的六种利用方式
- Network protocol learning -- lldp protocol learning
- IdentityServer4 定义概念
- keep-alive
- [the path of system analyst] Chapter 6: Double inventory demand engineering (comprehensive knowledge concept)
- 炒股票开户的话,手机开户安全吗?有谁知道啊?
- How to install SSL certificates in Microsoft Exchange 2010
- tokenizers>=0.11.1,!= 0.11.3,<0.13 is required for a normal functioning of this module,
- Task03 probability theory
猜你喜欢

【论文阅读|深度】Role-based network embedding via structural features reconstruction with degree-regularized

OpenCV学习(一)---环境搭建

Modbus protocol and serialport port read / write

I hope to explain the basics of canvas as clearly as possible according to my ideas

无心剑中译伊玛·拉扎罗斯《新巨人·自由女神》

Your driver settings have been set to force 4x antialiasing in OpenGL applications
![[200 opencv routines] 210 Are there so many holes in drawing a straight line?](/img/1e/5b8245eb1c391649c7b2783c62c2b0.png)
[200 opencv routines] 210 Are there so many holes in drawing a straight line?

1-7snapshots and clones in VMWare

Es learning

CSRF attack
随机推荐
【论文阅读|深度】Role-based network embedding via structural features reconstruction with degree-regularized
Comparison and evaluation of digicert and globalsign single domain ov SSL certificates
Is it safe to speculate in stocks by mobile phone?
OODA工作法
Use of Siemens plcs7-200 (I) -- Introduction to development environment and configuration software
1-7snapshots and clones in VMWare
ES 学习
CSRF攻击
MCU development -- face recognition application based on esp32-cam
Sign up to open the third session of the "flying oar hacker marathon". It's been a long time
[200 opencv routines] 210 Are there so many holes in drawing a straight line?
报名开启|飞桨黑客马拉松第三期如约而至,久等啦
【OpenCV 例程200篇】210. 绘制直线也会有这么多坑?
戴尔科技演绎“快”字诀,玩转CI/CD
[paper reading | deep reading] line: large scale information network embedding
【历史上的今天】6 月 24 日:网易成立;首届消费电子展召开;世界上第一次网络直播
Server rendering
Is it safe to open an account through mobile phone if you open an account through stock speculation? Who knows?
What is CRA
Chinese translation of IMA Lazarus' the new giant, the goddess of Liberty