我目前正在使用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) 有没有通用的方法告诉 Gson 不要写空字符串?
我非常不喜欢必须实现一个 TypeAdapter 来处理每个字段,正如这里的答案所暗示的那样。