当前位置:网站首页>@Serializedname annotation use
@Serializedname annotation use
2022-07-05 10:12:00 【A bird carved in the desert】
Sometimes the server returns us some field names that are not suitable for java bean The name of , We can use @SerializedName annotation , To alias the fields returned by the server .
For example, the following server returns json,1234 It's a bunch of meaningless names , And we need to be clear 1234 The meaning of each field , At this time, taking an alias can be very convenient for subsequent development :
{
"Result": {
"State": "200",
"Msg": " success "
},
"Data": {
"1": "100",
"2": "CHECKED",
"3": "UPDATE",
"4": 201901021130,
"5": 3.56
}
}
stay Android In development , Use of third parties Gson Package parsing json By default, when an object is created, it will json Corresponding fields in the file , Resolved to java The attribute field in the code .
image Result The label can put json The data is parsed into java object , however Data The data in shows a confused face , It's impossible to write such a class ?
That's not gonna work , But how can the front end be parsed when the background returns like this ?
because JSON Some of the fields in may not be appropriate to be used directly as Java Field , So here we use @SerializedName Annotation to make JSON Fields and Java Establish a mapping relationship between fields
In order to make it easier for others to read the code , We're writing correspondence Java Class time , You can use @SerializedName Make the code more friendly .
/**
* 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 annotation _yushuangping The blog of -CSDN Blog _serializedname annotation
@SerializedName Annotation fault tolerance _13468207 Technology blog _51CTO Blog
Android In development Gson object @SerializedName Use of annotations - Simple books
边栏推荐
- Flutter development: use safearea
- Getting started with Apache dolphin scheduler (one article is enough)
- Apache DolphinScheduler 入门(一篇就够了)
- QT timer realizes dynamic display of pictures
- Implementation of smart home project
- Pagoda panel MySQL cannot be started
- Node red series (29): use slider and chart nodes to realize double broken line time series diagram
- La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
- How Windows bat script automatically executes sqlcipher command
- Analysis on the wallet system architecture of Baidu trading platform
猜你喜欢
学习笔记5--高精地图解决方案
《天天数学》连载58:二月二十七日
Constraintlayout officially provides rounded imagefilterview
Comment obtenir le temps STW du GC (collecteur d'ordures)?
程序员搞开源,读什么书最合适?
Getting started with Apache dolphin scheduler (one article is enough)
QT timer realizes dynamic display of pictures
Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
学习笔记6--卫星定位技术(上)
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
随机推荐
Apache dolphin scheduler system architecture design
【C语言】动态内存开辟的使用『malloc』
Windows uses commands to run kotlin
《微信小程序-基础篇》小程序中的事件与冒泡
Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
Small program startup performance optimization practice
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下
oracle和mysql批量Merge对比
Energy momentum: how to achieve carbon neutralization in the power industry?
leetcode:1200. 最小绝对差
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
Wechat applet - simple diet recommendation (2)
isEmpty 和 isBlank 的用法区别
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
ConstraintLayout的流式布局Flow
如何获取GC(垃圾回收器)的STW(暂停)时间?
横向滚动的RecycleView一屏显示五个半,低于五个平均分布
Node red series (29): use slider and chart nodes to realize double broken line time series diagram
MySQL字符类型学习笔记