当前位置:网站首页>@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

原网站

版权声明
本文为[A bird carved in the desert]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050941162220.html