当前位置:网站首页>@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注解
边栏推荐
- (1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
- RMS to EAP is simply implemented through mqtt
- [system design] index monitoring and alarm system
- Node red series (29): use slider and chart nodes to realize double broken line time series diagram
- 基于单片机步进电机控制器设计(正转反转指示灯挡位)
- Wechat applet - simple diet recommendation (2)
- 分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
- 从“化学家”到开发者,从甲骨文到TDengine,我人生的两次重要抉择
- C#函数返回多个值方法
- Design and exploration of Baidu comment Center
猜你喜欢
Windows uses commands to run kotlin
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
The comparison of every() and some() in JS uses a power storage plan
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
Apache dolphin scheduler system architecture design
Understand the window query function of tdengine in one article
Cent7 Oracle database installation error
tongweb设置gzip
单片机原理与接口技术(ESP8266/ESP32)机器人类草稿
如何获取GC(垃圾回收器)的STW(暂停)时间?
随机推荐
Getting started with Apache dolphin scheduler (one article is enough)
Comparison of batch merge between Oracle and MySQL
善用兵者,藏于无形,90 分钟深度讲解最佳推广价值作品
Kotlin compose multiple item scrolling
The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name
Tianlong Babu TLBB series - about items dropped from packages
如何獲取GC(垃圾回收器)的STW(暫停)時間?
如何获取GC(垃圾回收器)的STW(暂停)时间?
高级 OpenCV:BGR 像素强度图
Pagoda panel MySQL cannot be started
The essence of persuasion is to remove obstacles
C#函数返回多个值方法
. Net delay queue
Swift tableview style (I) system basic
tongweb设置gzip
分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
Coffeescript Chinese character to pinyin code
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
On July 2, I invite you to TD Hero online press conference