当前位置:网站首页>@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
边栏推荐
- Personal website construction tutorial | local website environment construction | website production tutorial
- Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
- ConstraintLayout的流式布局Flow
- 美图炒币半年亏了3个亿,华为被曝在俄罗斯扩招,AlphaGo的同类又刷爆一种棋,今日更多大新闻在此...
- Generics, generic defects and application scenarios that 90% of people don't understand
- La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
- [200 opencv routines] 219 Add digital watermark (blind watermark)
- Flutter development: a way to solve the problem of blank space on the top of listview
- Kotlin compose and native nesting
- Z-blog template installation and use tutorial
猜你喜欢
硬核,你见过机器人玩“密室逃脱”吗?(附代码)
程序员搞开源,读什么书最合适?
Analysis on the wallet system architecture of Baidu trading platform
学习笔记6--卫星定位技术(上)
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
让AI替企业做复杂决策真的靠谱吗?参与直播,斯坦福博士来分享他的选择|量子位·视点...
Six simple cases of QT
Hard core, have you ever seen robots play "escape from the secret room"? (code attached)
Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft
把欧拉的创新带向世界 SUSE 要做那个引路人
随机推荐
The essence of persuasion is to remove obstacles
【小技巧】獲取matlab中cdfplot函數的x軸,y軸的數值
Energy momentum: how to achieve carbon neutralization in the power industry?
Personal website construction tutorial | local website environment construction | website production tutorial
Swift tableview style (I) system basic
Theme. AppCompat. Light. Darkactionbar not found
Mysql80 service does not start
[system design] index monitoring and alarm system
单片机原理与接口技术(ESP8266/ESP32)机器人类草稿
Six simple cases of QT
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
Node-RED系列(二九):使用slider与chart节点来实现双折线时间序列图
Click the picture in the mobile browser and the picture will not pop up
最全是一次I2C总结
程序员搞开源,读什么书最合适?
盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
QT event filter simple case
H. 265 introduction to coding principles
ThreadLocal source code learning
Baidu app's continuous integration practice based on pipeline as code