当前位置:网站首页>@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
边栏推荐
- Z-blog template installation and use tutorial
- Kotlin Compose 多个条目滚动
- [NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
- 90%的人都不懂的泛型,泛型的缺陷和应用场景
- Node-RED系列(二九):使用slider与chart节点来实现双折线时间序列图
- Coordinate system of view
- Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下
- Windows uses commands to run kotlin
- The comparison of every() and some() in JS uses a power storage plan
- Kotlin compose multiple item scrolling
猜你喜欢
A high density 256 channel electrode cap for dry EEG
Roll up, break 35 - year - old Anxiety, animation Demonstration CPU recording Function call Process
Wechat applet - simple diet recommendation (3)
RMS TO EAP通过MQTT简单实现
Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft
. Net delay queue
学习笔记4--高精度地图关键技术(下)
90%的人都不懂的泛型,泛型的缺陷和应用场景
(1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
How to get the STW (pause) time of GC (garbage collector)?
随机推荐
Fluent generates icon prompt logo widget
mysql80服务不启动
如何判断线程池已经执行完所有任务了?
Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
苹果 5G 芯片研发失败?想要摆脱高通为时过早
《微信小程序-基础篇》小程序中的事件与冒泡
[C language] the use of dynamic memory development "malloc"
Openes version query
isEmpty 和 isBlank 的用法区别
高级 OpenCV:BGR 像素强度图
《天天数学》连载58:二月二十七日
LiveData 面试题库、解答---LiveData 面试 7 连问~
把欧拉的创新带向世界 SUSE 要做那个引路人
程序员如何活成自己喜欢的模样?
Wechat applet - simple diet recommendation (4)
Flutter development: use safearea
RMS to EAP is simply implemented through mqtt
Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
Personal website construction tutorial | local website environment construction | website production tutorial
天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题