当前位置:网站首页>@SerializedName注解使用
@SerializedName注解使用
2022-07-05 09:41:00 【沙漠一只雕得儿得儿】
有时服务端返回给我们一些字段名不适合作为java bean的名称,我们可以使用@SerializedName注解,来给服务端返回的字段取别名。
例如下面服务端返回的json,1234是一堆无意义的名称,而我们需要明确1234每个字段的含义,这时取个别名就可以很方便后续的开发:
{
"Result": {
"State": "200",
"Msg": "成功"
},
"Data": {
"1": "100",
"2": "CHECKED",
"3": "UPDATE",
"4": 201901021130,
"5": 3.56
}
}在Android开发中,使用第三方Gson包解析json成对象时默认的是将json文件里对应字段,解析到java代码里的属性字段里面。
像Result标签可以将json数据解析成java对象,但是Data中的数据表示一脸懵,总不可能写一个这样的类吧?

这样肯定是不行的,但是后台这样返回前端该怎么解析呢?
因为JSON中的一些字段可能不太适合直接作为Java字段来命名,因此这里使用了@SerializedName注解的方式来让JSON字段和Java字段之间建立映射关系
为了更加利于别人阅读代码,我们在写对应Java类时,就可以使用@SerializedName让代码变的更加友好。
/**
* Created by ysp
* on 2021/8/16
*/
public class TestBean {
/**
* 1 : 100
* 2 : CHECKED
* 3 : UPDATE
* 4 : 201901021130
* 5 : 3.56
*/
@SerializedName("1")
private String number;
@SerializedName("2")
private String isCheck;
@SerializedName("3")
private String status;
@SerializedName("4")
private long date;
@SerializedName("5")
private double percent;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getIsCheck() {
return isCheck;
}
public void setIsCheck(String isCheck) {
this.isCheck = isCheck;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
public double getPercent() {
return percent;
}
public void setPercent(double percent) {
this.percent = percent;
}
}@SerializedName注解_yushuangping的博客-CSDN博客_serializedname注解
边栏推荐
- [C language] the use of dynamic memory development "malloc"
- 解决idea调试过程中liquibase – Waiting for changelog lock….导致数据库死锁问题
- [system design] index monitoring and alarm system
- MySQL character type learning notes
- Node-RED系列(二九):使用slider与chart节点来实现双折线时间序列图
- The writing speed is increased by dozens of times, and the application of tdengine in tostar intelligent factory solution
- [200 opencv routines] 219 Add digital watermark (blind watermark)
- 卷起来,突破35岁焦虑,动画演示CPU记录函数调用过程
- How to implement complex SQL such as distributed database sub query and join?
- Theme. AppCompat. Light. Darkactionbar not found
猜你喜欢

Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)

How to implement complex SQL such as distributed database sub query and join?

盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名

程序员搞开源,读什么书最合适?

From "chemist" to developer, from Oracle to tdengine, two important choices in my life

Swift set pickerview to white on black background

Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)

解决idea调试过程中liquibase – Waiting for changelog lock….导致数据库死锁问题

Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下

Unity particle special effects series - the poison spray preform is ready, and the unitypackage package can be used directly - next
随机推荐
Solve liquibase – waiting for changelog lock Cause database deadlock
ArcGIS Pro 创建要素
Comment obtenir le temps STW du GC (collecteur d'ordures)?
Jupiter notebook shortcut key
Tianlong Babu TLBB series - about items dropped from packages
正式上架!TDengine 插件入驻 Grafana 官网
Tdengine offline upgrade process
Kotlin Compose 与原生 嵌套使用
RMS TO EAP通过MQTT简单实现
百度智能小程序巡檢調度方案演進之路
How to get the STW (pause) time of GC (garbage collector)?
QT realizes signal transmission and reception between two windows
历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
Implementation of smart home project
搞数据库是不是越老越吃香?
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package can be used directly - next
MySQL digital type learning notes
Tianlong Babu TLBB series - questions about skill cooling and the number of attack ranges
Data visualization platform based on template configuration