当前位置:网站首页>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
边栏推荐
- OpenCV学习(一)---环境搭建
- 報名開啟|飛槳黑客馬拉松第三期如約而至,久等啦
- Google Earth Engine(GEE)——evaluate实现一键批量下载研究区内的所有单张影像(上海市部分区域)
- Performance memory
- Flask blog practice - archiving and labeling of sidebar articles
- Advanced single chip microcomputer -- development of PCB (2)
- Your driver settings have been set to force 4x antialiasing in OpenGL applications
- A random number generator
- How to install SSL certificates in Microsoft Exchange 2010
- NuxtJS实战案例
猜你喜欢
![[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?
![[paper reading | deep reading] line: large scale information network embedding](/img/83/a67e490603c81c1d8ad9302c922769.png)
[paper reading | deep reading] line: large scale information network embedding

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

Network protocol learning -- lldp protocol learning

软件测试 避免“试用期被辞退“指南,看这一篇就够了

网易开源的分布式存储系统 Curve 正式成为 CNCF 沙箱项目

Kotlin arrays and collections (1) {create arrays, use arrays, use for in loops to traverse arrays, use array indexes, and multi-dimensional arrays}

FPGA基于VGA显示字符及图片

【论文阅读|深读】LINE: Large-scale Information Network Embedding

Task03 probability theory
随机推荐
June 24, 2022: golang multiple choice question, what does the following golang code output? A:1; B:3; C:4; D: Compilation failed. package main import ( “fmt“ ) func mai
Comparison and evaluation of digicert and globalsign single domain ov SSL certificates
软件测试 避免“试用期被辞退“指南,看这一篇就够了
[file containing vulnerability-03] six ways to exploit file containing vulnerabilities
视频会议一体机的技术实践和发展趋势
VW VH adaptation of mobile terminal
Five types of questions about network planning
3 Questions par jour (3) - vérifier l'existence d'entiers et de leurs doubles
[200 opencv routines] 210 Are there so many holes in drawing a straight line?
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-7
服务端渲染
Garbage collection mechanism
How to install SSL certificates in Microsoft Exchange 2010
SystemVerilog(十三)-枚举数据类型
Output reading: apply what you have learned
ES 学习
Android:kotlin中Gson与JSON的泛型映射解析
Google Earth Engine(GEE)——evaluate實現一鍵批量下載研究區內的所有單張影像(上海市部分區域)
OODA working method
Network protocol learning -- lldp protocol learning