0

今天我用的是FastJson( https://github.com/alibaba/fastjson ),下面是我的demo代码。

class User {
  private final String name;
  private final int age;
  private final String birthday;
  private String password;

  public User(String name, int age, String birthday) {
    this.name = name;
    this.age = age;
    this.birthday = birthday;
  }

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

  public String getName() {
    return name;
  }

  public int getAge() {
    return age;
  }

  public String getBirthday() {
    return birthday;
  }

  public String getPassword() {
    return password;
  }

  @Override
  public String toString() {
    return "User{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", birthday='" + birthday + '\'' +
            ", password='" + password + '\'' +
            '}';
  }
}

它将序列化具有 getXXX 方法的字段。我不想序列化密码,我会调用 getPassword() 来获取密码值。

我不想重命名方法 getPassword 并将变量密码更新为 public。

有谁知道在序列化此类时如何忽略字段?

4

1 回答 1

0
@JSONField(serialze=false)
public String getPassword() {
  return password;
}
于 2021-06-10T03:04:31.617 回答