您可以
@Expose使用使用GSON
注释获得类似的效果
GsonBuilder.excludeFieldsWithoutExposeAnnotation()。
例如
public class User { @Expose private String firstName; @Expose(serialize = false) private String lastName; @Expose (serialize = false, deserialize = false) private String emailAddress; private String password; }如果
Gson gson = newGsonBuilder().excludeFieldsWithoutExposeAnnotation().create()与上述类一起使用,则
toJson()和
fromJson()方法将完全忽略密码字段,因为它没有
@Expose注释。
(请注意,在这里您还可以获得更细粒度的控制,因为您还可以控制GSON是否对字段进行序列化/反序列化)。
参考:https : //github.com/google/gson/blob/master/UserGuide.md#TOC-Gson-s-
Expose



