gpt4 book ai didi

java - 如何排除 Java 对象属性作为 JSON 字符串的一部分?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:42:24 24 4
gpt4 key购买 nike

我有一个 Java POJO。很少有属性与列表<>一起存在。将此对象转换为 JSON 字符串时,我想排除列表属性,那么为此使用什么注解呢?

public class StudentResultSummary {
private String totMarks;
private String avgMarks;
private List<StudentResult> resultList = new ArrayList<StudentResult>();
}

转换为 JSON:

StudentResultSummary resultSummary = new StudentResultSummary();
Json json = new Json();
policySummary = json.encode(resultSummary);

如何确保字段 resultList 不包含在 JSON 响应中?

最佳答案

来自 Chris Seline's answer :

Any fields you don't want serialized in general you should use the "transient" modifier, and this also applies to json serializers (at least it does to a few that I have used, including gson).

If you don't want name to show up in the serialized json give it a transient keyword, eg:

private transient String name;

关于java - 如何排除 Java 对象属性作为 JSON 字符串的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33893481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com