当前位置:网站首页>@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注解
边栏推荐
- Pagoda panel MySQL cannot be started
- . Net delay queue
- Tongweb set gzip
- 卷起来,突破35岁焦虑,动画演示CPU记录函数调用过程
- Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
- To bring Euler's innovation to the world, SUSE should be the guide
- 美图炒币半年亏了3个亿,华为被曝在俄罗斯扩招,AlphaGo的同类又刷爆一种棋,今日更多大新闻在此...
- [tips] get the x-axis and y-axis values of cdfplot function in MATLAB
- How to use sqlcipher tool to decrypt encrypted database under Windows system
- TDengine 离线升级流程
猜你喜欢

RMS to EAP is simply implemented through mqtt

Tongweb set gzip
![[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution](/img/f3/782246100bca3517d95869be80d9c5.png)
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution

Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)

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

Cut off 20% of Imagenet data volume, and the performance of the model will not decline! Meta Stanford et al. Proposed a new method, using knowledge distillation to slim down the data set

Tdengine can read and write through dataX, a data synchronization tool

90%的人都不懂的泛型,泛型的缺陷和应用场景

Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)

如何獲取GC(垃圾回收器)的STW(暫停)時間?
随机推荐
Swift tableview style (I) system basic
QT realizes signal transmission and reception between two windows
Tutorial on building a framework for middle office business system
Kotlin compose and native nesting
Oracle combines multiple rows of data into one row of data
RMS TO EAP通过MQTT简单实现
Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft
为什么不建议你用 MongoDB 这类产品替代时序数据库?
【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
.Net之延迟队列
Node red series (29): use slider and chart nodes to realize double broken line time series diagram
Roll up, break 35 - year - old Anxiety, animation Demonstration CPU recording Function call Process
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
单片机原理与接口技术(ESP8266/ESP32)机器人类草稿
Common fault analysis and Countermeasures of using MySQL in go language
能源势动:电力行业的碳中和该如何实现?
Small program startup performance optimization practice
美图炒币半年亏了3个亿,华为被曝在俄罗斯扩招,AlphaGo的同类又刷爆一种棋,今日更多大新闻在此...
如何獲取GC(垃圾回收器)的STW(暫停)時間?