Jackson:使用jackson将json字符串响应转换为pojo转换时过滤空值或空值

Tej*_*ani 3 json pojo liferay jackson liferay-6

我正在使用杰克逊将杰森的回应转换为pojo列表.以下是我得到的回复.

[

    {
        "code": "",
        "total": 24,
        "name": null
    },
    {
        "code": "",
        "total": 1,
        "name": "Test"
    }
]
Run Code Online (Sandbox Code Playgroud)

我正在将它转换为Pojo列表.下面是我的pojo.

public class ItemCategory {

private String code;
private String name;
private String total;

public ItemCategory() {
}

public ItemCategory(final String code, final String name, final String total) {
    super();
    this.code = code;
    this.name = name;
    this.total = total;
}

/**
 * @return the code
 */
public String getCode() {
    return code;
}

/**
 * @param code
 *            the code to set
 */
public void setCode(final String code) {
    this.code = code;
}

/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name
 *            the name to set
 */
public void setName(final String name) {
    this.name = name;
}

/**
 * @return the count
 */
public String getTotal() {
    return total;
}

/**
 * @param count
 *            the count to set
 */
public void setTotal(final String total) {
    this.total = total;
}
}
Run Code Online (Sandbox Code Playgroud)

一切正常.但我想删除要转换为pojo的值,其代码为空/空值.即"代码":"",或"代码":null

我使用下面的jackson代码将json转换为pojo.

Object pojo = null;
try {
    pojo = mapper.readValue(jsonString, typeReference);
} catch (JsonParseException e) {
    throw new InvalidPojoException(e.toString(), e);
} catch (JsonMappingException e) {
    throw new InvalidPojoException(e.toString(), e);
} catch (IOException e) {
    throw new InvalidPojoException(e.toString(), e);
} catch (Exception e) {
    throw new InvalidPojoException(e.toString(), e);
}
return pojo;
Run Code Online (Sandbox Code Playgroud)

使用以下代码为json对象.

(List<ItemCategory>) JsonParserUtil.toPojo(serviceResponse.getStringResponse(),new TypeReference<List<ItemCategory>>(){});
Run Code Online (Sandbox Code Playgroud)

任何指针都会受到赞赏.

提前致谢.

Mic*_*l M 7

你可能想要像这样注释你的bean类:

@JsonSerialize(
include=JsonSerialize.Inclusion.NON_NULL,
)
Run Code Online (Sandbox Code Playgroud)

source:JsonSerialize注释javadoc