当前位置:网站首页>@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注解
边栏推荐
- QT realizes signal transmission and reception between two windows
- oracle 多行数据合并成一行数据
- Roll up, break 35 - year - old Anxiety, animation Demonstration CPU recording Function call Process
- How to get the STW (pause) time of GC (garbage collector)?
- Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
- 程序员如何活成自己喜欢的模样?
- Design and exploration of Baidu comment Center
- Openes version query
- The writing speed is increased by dozens of times, and the application of tdengine in tostar intelligent factory solution
- 天龙八部TLBB系列 - 单体技能群伤
猜你喜欢
Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
美图炒币半年亏了3个亿,华为被曝在俄罗斯扩招,AlphaGo的同类又刷爆一种棋,今日更多大新闻在此...
Swift set pickerview to white on black background
Hard core, have you ever seen robots play "escape from the secret room"? (code attached)
Kotlin compose and native nesting
mysql80服务不启动
How to get the STW (pause) time of GC (garbage collector)?
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package can be used directly - next
Kotlin compose multiple item scrolling
随机推荐
[technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
cent7安装Oracle数据库报错
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
QT timer realizes dynamic display of pictures
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,我人生的两次重要抉择
Swift uses userdefaults and codable to save an array of class objects or structure instances
Resolve the horizontal (vertical) sliding conflict between viewpager and WebView
Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
Apache dolphin scheduler system architecture design
Hard core, have you ever seen robots play "escape from the secret room"? (code attached)
Cent7 Oracle database installation error
MySQL character type learning notes
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下
How to use sqlcipher tool to decrypt encrypted database under Windows system
Analysis on the wallet system architecture of Baidu trading platform
Baidu app's continuous integration practice based on pipeline as code
idea用debug调试出现com.intellij.rt.debugger.agent.CaptureAgent,导致无法进行调试
【C语言】动态内存开辟的使用『malloc』