Google Gson 属性重命名(@SerializedName 注解)

x33g5p2x  于2021-12-25 转载在 Go  
字(3.9k)|赞(0)|评价(0)|浏览(772)

本文内容大多基于官方文档和网上前辈经验总结,经过个人实践加以整理积累,仅供参考。

默认情况下解析和生成 JSON 字符串要求键名称和 POJO 类的属性名一一对应,实际应用中常常出现不一致现象,

如期望字符串:{“account”:”admin”,”password”:”123456”,”age”:30}

实际字符串:{“account”:”admin”,”code”:”123456”,”age”:30}

Gson 使用 @SerializedName 注解解决 JSON 字符串键名称和 POJO 类属性名不对应的问题

示例

1 POJO 类

import com.google.gson.annotations.SerializedName;

public class User {

    private String account;

    @SerializedName("code")
    private String password;

    private String name;

    private int age;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User [account=" + account + ", password=" + password + ", name=" + name + ", age=" + age + "]";
    }

}

2 单元测试

@Test
public void test() {
    Gson gson = new Gson();

    String userStr = "{\"account\":\"admin\",\"code\":\"123456\",\"age\":30}";

    User user = gson.fromJson(userStr, User.class);

    assertEquals(user.getAccount(), "admin");
    assertEquals(user.getPassword(), "123456");
    assertEquals(user.getAge(), 30);
}

如果存在多个需要解析的 JSON 字符串,每个字符串的键名称各不相同,但实际上对应的都是 POJO 对象的同一个属性,如

{“account”:”admin”,”password”:”123456”,”age”:30}

{“account”:”admin1”,”code”:”000000”,”age”:20}

{“account”:”adminX”,”xxx”:”aaaaaa”,”age”:40}

{“account”:”adminY”,”yyy”:”bbbbbb”,”age”:45}

{“account”:”adminZ”,”zzz”:”cccccc”,”age”:50}

解决方法:

1 修改 POJO 类

import com.google.gson.annotations.SerializedName;

public class User {

    private String account;

    @SerializedName(value = "password", alternate = {"code", "xxx", "yyy", "zzz"})
    private String password;

    private String name;

    private int age;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User [account=" + account + ", password=" + password + ", name=" + name + ", age=" + age + "]";
    }

}

2 单元测试

@Test
public void test() {
    Gson gson = new Gson();

    String userStr = "{\"account\":\"admin\",\"password\":\"123456\",\"age\":30}";
    User user = gson.fromJson(userStr, User.class);
    assertEquals(user.getAccount(), "admin");
    assertEquals(user.getPassword(), "123456");
    assertEquals(user.getAge(), 30);

    userStr = "{\"account\":\"admin1\",\"code\":\"000000\",\"age\":20}";
    user = gson.fromJson(userStr, User.class);
    assertEquals(user.getAccount(), "admin1");
    assertEquals(user.getPassword(), "000000");
    assertEquals(user.getAge(), 20);

    userStr = "{\"account\":\"adminX\",\"xxx\":\"aaaaaa\",\"age\":40}";
    user = gson.fromJson(userStr, User.class);
    assertEquals(user.getAccount(), "adminX");
    assertEquals(user.getPassword(), "aaaaaa");
    assertEquals(user.getAge(), 40);

    userStr = "{\"account\":\"adminY\",\"yyy\":\"bbbbbb\",\"age\":45}";
    user = gson.fromJson(userStr, User.class);
    assertEquals(user.getAccount(), "adminY");
    assertEquals(user.getPassword(), "bbbbbb");
    assertEquals(user.getAge(), 45);

    userStr = "{\"account\":\"adminZ\",\"zzz\":\"cccccc\",\"age\":50}";
    user = gson.fromJson(userStr, User.class);
    assertEquals(user.getAccount(), "adminZ");
    assertEquals(user.getPassword(), "cccccc");
    assertEquals(user.getAge(), 50);
}

相关文章