我目前正在使用jackson 2.1.4,在将对象转换为JSON字符串时,忽略字段时遇到了一些问题.

下面是我的类,它充当要转换的对象:

public class JsonOperation {

public static class Request {
    @JsonInclude(Include.NON_EMPTY)
    String requestType;
    Data data = new Data();

    public static class Data {
        @JsonInclude(Include.NON_EMPTY)
        String username;
        String email;
        String password;
        String birthday;
        String coinsPackage;
        String coins;
        String transactionId;
        boolean isLoggedIn;
    }
}

public static class Response {
    @JsonInclude(Include.NON_EMPTY)
    String requestType = null;
    Data data = new Data();

    public static class Data {
        @JsonInclude(Include.NON_EMPTY)
        enum ErrorCode { ERROR_INVALID_LOGIN, ERROR_USERNAME_ALREADY_TAKEN, ERROR_EMAIL_ALREADY_TAKEN };
        enum Status { ok, error };

        Status status;
        ErrorCode errorCode;
        String expiry;
        int coins;
        String email;
        String birthday;
        String pictureUrl;
        ArrayList <Performer> performer;
    }
}
}

下面是我如何转换它的:

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

JsonOperation subscribe = new JsonOperation();

subscribe.request.requestType = "login";

subscribe.request.data.username = "Vincent";
subscribe.request.data.password = "test";


Writer strWriter = new StringWriter();
try {
    mapper.writeValue(strWriter, subscribe.request);
} catch (JsonGenerationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (JsonMappingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Log.d("JSON", strWriter.toString())

以下是输出:

{"data":{"birthday":null,"coins":null,"coinsPackage":null,"email":null,"username":"Vincent","password":"test","transactionId":null,"isLoggedIn":false},"requestType":"login"}

如何避免这些空值?我只想获取"订阅"所需的信息!

这正是我想要的结果:

{"data":{"username":"Vincent","password":"test"},"requestType":"login"}

我还try 了@JsonInclude(Include.NON_NULL)并将所有变量设置为NULL,但也没有成功!谢谢你们的帮助!

推荐答案

您将注释放在了错误的位置-它需要放在类上,而不是字段上.即:

@JsonInclude(Include.NON_NULL) //or Include.NON_EMPTY, if that fits your use case 
public static class Request {
  // ...
}

如注释中所述,在低于2.x的版本中,此注释的语法为:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) // or JsonSerialize.Inclusion.NON_EMPTY

另一个选项是直接配置ObjectMapper,只需调用

(郑重声明,我认为这个答案的受欢迎程度表明此注释should适用于逐个字段,@fasterxml)

Java相关问答推荐

在applitcation.properties中找到的字符串中输入变量

当耗时的代码完成时,Circular ProgressIndicator显示得太晚

日食IDE 2024-03在Ubuntu下崩溃,导致hr_err_pid.log

虚拟线程似乎在外部服务调用时阻止运营商线程

为什么Java中的两个日期有差异?

scanner 如何在执行hasNextLine一次后重新读取整个文件?

为什么Java的代码工作(if condition内部的实例)

springboot start loge change

需要一个找不到的jakarta.sistence.EntityManager类型的Bean

我需要生成一个文件来整合每个特性执行的所有JSON结果

内存中的H2修剪尾随空格

更新AWS凭据

匹配一组字符或另一组字符

Spring-Boot Kafka应用程序到GraalVM本机映像-找不到org.apache.kafka.streams.processor.internals.DefaultKafkaClientSupplier

使用Jackson库反序列化json

与IntArray相比,ArrayList<;Int>;对于大量元素的性能极差

将BlockingQueue+守护程序线程替换为执行器

Java递归泛型是否可以被视为继承和重写的语法糖

由于版本不匹配,从Java 8迁移到Java 17和Spring 6 JUnit4失败

如何用Micrometer&;斯普肯