我在用杰克逊图书馆.

I want to ignore a specific field when serializing/deserializing, so for example:

public static class Foo {
    public String foo = "a";
    public String bar = "b";

    @JsonIgnore
    public String foobar = "c";
}

你应该给我:

{
foo: "a",
bar: "b",
}

But I'm getting:

{
foo: "a",
bar: "b",
foobar: "c"
}

我使用以下代码序列化对象:

ObjectMapper mapper = new ObjectMapper();
String out = mapper.writeValueAsString(new Foo());

The real type of the field on my class is an instance of the Log4J Logger class. What am I doing wrong?

推荐答案

Ok, so for some reason I missed this answer.

以下代码按预期工作:

@JsonIgnoreProperties({"foobar"})
public static class Foo {
    public String foo = "a";
    public String bar = "b";

    public String foobar = "c";
}

//Test code
ObjectMapper mapper = new ObjectMapper();
Foo foo = new Foo();
foo.foobar = "foobar";
foo.foo = "Foo";
String out = mapper.writeValueAsString(foo);
Foo f = mapper.readValue(out, Foo.class);

Json相关问答推荐

Google Page to JSON应用程序脚本出现CORS错误

JSON字符串处理注入引号

PowerShell脚本未按预期生成预期的JSON输出

如何使用PowerShell从ExchangeOnline命令执行中获得JSON输出

使用jq过滤复杂json对象中的数据

Vega-Lite规范:尽管在规范中提供了数据,但显示空图表

如何编写 jolt 规范以将不同的对象转换为数组

xidel:是否可以从 JSON 对象中检索特定的嵌套值?

Powershell v7 文本背景突出显示

如何将该 JSON 解析为 Kotlin 类?

使用 REACT 从字段值中读取 JSON 元素

派生类的属性没有得到价值

Go - JSON 验证抛出错误,除非我在 struct 中使用指针.为什么?

使用 jq 将键值行转换为 json

Laravel5 Json 获取文件内容

编写 JSON 日志(log)文件的格式?

通过 RestAssured 中的 JsonPath 访问匿名数组的元素

如何在 Rails 模型中验证字符串是否为 json

通过 JSON 发送 HTML 代码

如何在dart Flutter 中将json字符串转换为json对象?