为了获得此结果,您需要使用注释所有字段
@Expose:
public class ConfigInstance { @Expose public String database_address; @Expose public int database_port; @Expose public String database_user; @Expose(serialize = false) private String database_pass; @Expose public String database_pass_hash;并将Gson配置为仅公开带注释的字段,并忽略其余字段,如下所示:
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
然后,您将获得:
{ "database_address": "127.0.0.1", "database_port": 1521, "database_user": "test", "database_pass_hash": "B9FE2C011B59F0D0D383D70073E48A19"}同样,在反序列化字符串时,您还将仍然具有password属性。
不过,您仍然可以配置Gson序列化器来完成此操作。



