当前位置:网站首页>Android: generic mapping analysis of gson and JSON in kotlin
Android: generic mapping analysis of gson and JSON in kotlin
2022-06-25 11:02:00 【Running snail, Z】
background :
stay Android in ,kotlin Use , It can greatly beautify the code and bring simplicity to the code , however kotlin Before ,android The main language is Java.
stay android in , Parsing data is a common technique .JavaBean,GSON,JSONOBJECT, These three members are partners , But in kotlin This is still supported in , however kotlin Of data GSON,JSONOBJECT, And Java Parsing is different
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":" male "}
The analysis is as follows
People people=new Gson().fromJson(json,People.class);
This is a Java Parsing .
kotlin:
kotlin Analysis and Java Different ,JavaBean stay kotlin Middle call data data
data class People(var name:String="",var sex:String="");
Or directly define a class Class is OK
class People{
var name:String=""
var sex:String=""
}
analysis GSON:
When we are doing parsing, we'd better be generic , So it's a little different to do parsing tools , as follows
object GsonUtil {
public inline fun <reified T> getGsonObject(json: String): T? {
val result = Gson().fromJson<T>(json)
return result
}
//type Generic extension
inline fun <reified T> Gson.fromJson(json: String) = this.fromJson<T>(json, object : TypeToken<T>() {}.type)
}GSON stay kotlin The biggest generic handling of is type part , So we use the extension function internally , Yes Gson.fromJson Expand , In this way, we can use method parameter generic parsing directly .
This is different from Java Function generics of ,kotlin Support method generics .
summary :
Difference is that bean The definition of ,kotlin It's called in Chinese data class
边栏推荐
- Macro application connector\
- XSS攻击
- Performance network
- New school: no fraud Economics
- Ouverture de l'inscription | le troisième marathon des hackers de pagaie est arrivé comme prévu.
- 单片机进阶---PCB开发之照葫芦画瓢(二)
- 指南针在上面开股票账户安全吗?
- TASK03|概率论
- 好好思考
- [paper reading | deep reading] drne:deep recursive network embedding with regular equivalence
猜你喜欢
![[image fusion] image fusion based on morphological analysis and sparse representation with matlab code](/img/ae/027fc1a3ce40b35090531370022c92.png)
[image fusion] image fusion based on morphological analysis and sparse representation with matlab code

网络远程访问的方式使用树莓派

Your driver settings have been set to force 4x antialiasing in OpenGL applications

Es learning

WPF prism framework

单片机进阶---PCB开发之照葫芦画瓢(二)
![[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?

视频会议一体机的技术实践和发展趋势

Opencv learning (II) -- installing opencv on raspberry pie

1-7snapshots and clones in VMWare
随机推荐
Dependent properties, dependent additional properties, and type conversions
宏的运用接续符\
输出式阅读法:把学到的知识用起来
无心剑中译伊玛·拉扎罗斯《新巨人·自由女神》
Cdn+cos ultra detailed steps for drawing bed construction
Simple use of SVN
Shen Lu, China Communications Institute: police open source Protocol - ofl v1.1 Introduction and Compliance Analysis
keep-alive
成长:如何深度思考与学习
中国信通院沈滢:字体开源协议——OFL V1.1介绍及合规要点分析
New school: no fraud Economics
A five-year technical Er, based on the real experience of these years, gives some suggestions to the fresh students
每日3題(3)-檢查整數及其兩倍數是否存在
【OpenCV 例程200篇】210. 绘制直线也会有这么多坑?
Sign up to open the third session of the "flying oar hacker marathon". It's been a long time
每日3题(2)- 找出数组中的幸运数
指南针在上面开股票账户安全吗?
Advanced single chip microcomputer -- development of PCB (2)
Google Earth Engine(GEE)——evaluate實現一鍵批量下載研究區內的所有單張影像(上海市部分區域)
tokenizers>=0.11.1,!= 0.11.3,<0.13 is required for a normal functioning of this module,