相关疑难解决方法(0)

杰克逊序列化:忽略空值(或null)

我目前正在使用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; …
Run Code Online (Sandbox Code Playgroud)

java json jackson

131
推荐指数
6
解决办法
27万
查看次数

Gson序列化一般忽略空字符串

有没有通用的方法告诉 Gson 不要写空字符串?

我非常不喜欢必须实现一个 TypeAdapter 来处理每个字段,正如这里的答案所暗示的那样。

java gson

5
推荐指数
1
解决办法
9660
查看次数

标签 统计

java ×2

gson ×1

jackson ×1

json ×1