Java到JSON:指定每个转换的哪些字段被排除在外
如前所述在这里(与杰克逊一起)
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(objectToBeConverted);
我知道我可以使用@jsonignore
注释将字段排除在JSON字符串中,但是如果我想多次将同一类转换为JSON,但是每次选择不同的领域要忽略?
例如,如果我有一堂课,
class Foo {
int a;
int b;
...
}
我可以做类似的事情
Foo foo = new Foo();
String json1 = ow.writeValueAsString(foo).excludeField('b');
String json2 = ow.writeValueAsString(foo).excludeField('a');
,以便所产生的字符串看起来像
// json1
{
a: 1234
}
// json2
{
b: 5678
}
杰克逊做不到,也许Gson可以吗?还是另一个图书馆?
As mentioned here, I know that I can convert Java objects to JSON (with Jackson)
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(objectToBeConverted);
I know that I can exclude fields from being included in the JSON string using the @JsonIgnore
annotation, but what if I want to convert the same class to JSON multiple times, but each time choosing different fields to ignore?
For example, if I have a class
class Foo {
int a;
int b;
...
}
can I do something like
Foo foo = new Foo();
String json1 = ow.writeValueAsString(foo).excludeField('b');
String json2 = ow.writeValueAsString(foo).excludeField('a');
so that the resulting strings look like
// json1
{
a: 1234
}
// json2
{
b: 5678
}
If Jackson can't do it, maybe Gson can? Or another library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用不同的混合接口。我找到了两种方法。
使用方法来读取属性。然后,您可以创建一个仅定义要排除属性的混合类别:
使用
@jsonincludeproperties
不知道哪些属性to expude ,而是< em>包括:在这两种情况下,将该混合添加到对象中,映射器:
虽然有一件非常非常重要的事情 - 您必须为每个混合使用新的
ObjectMapper
。如果您使用ObjectMapper
实例来序列化foo
实例而无需混合插入,则添加混合物无济于事。这可能是因为ObjectMapper
实例缓存一些东西。You can try using different mix-in interfaces. I found two ways to do this.
Use methods for reading properties. You can then create a mix-in class that only defines the properties to exclude:
Use
@JsonIncludeProperties
to not tell which properties to exclude, but which properties to include:In both cases, add that mix-in to the object mapper:
There is one very, very important thing though - you must use a new
ObjectMapper
for each mix-in. If you use anObjectMapper
instance to serialize aFoo
instance without mix-ins, then adding the mix-in won't help. That's probably becauseObjectMapper
instances cache some stuff.如果您可以将所有原始类型装箱
foo
,这是一种简单的方法。例如:
int-&gt; Integer
,boolean-&gt;然后
,只需制作
foo
的副本,然后将要忽略的属性设置为null
。Here is a simple approach if you could box up all primitive type in
Foo
.For example:
int -> Integer
,boolean -> Boolean
Then, just make a copy of
Foo
and set the property which you want to ignore tonull
.根据您的需要,有多个解决方案:
首先:
您可以为自己的目的定义两个不同的 dto ,每次需要使用它。
第二:
您可以使用
@jsoninclude(jsoninclude.include.non_null)
属性注释:ps:您可以在类级上使用此注释,例如:
第三:
使用可以根据不同条件定义过滤器来忽略属性。
为您的过滤器定义一个简单的类:
然后将此过滤器添加为属性顶部的注释:
There is multiple solution based on your need:
First:
You can just define two different DTO for your purpose and every time you need to each one just use it.
Second:
You can use
@JsonInclude(JsonInclude.Include.NON_NULL)
annotation for the properties:P.S: You can use this annotation on class level as:
Third:
Use can define a filter to ignore properties based on different conditions.
Define a simple class for your filter:
And then add this filter as annotation on top of the property: