3

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

[

    {
        "code": "",
        "total": 24,
        "name": null
    },
    {
        "code": "",
        "total": 1,
        "name": "Test"
    }
]

我正在将其转换为 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;
}
}

一切正常。但我想删除要转换为 pojo 的值,它的代码为空白/空值。即“代码”:“”,或“代码”:空

我正在使用下面的杰克逊代码将 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;

使用下面的 json 代码来反对。

(List<ItemCategory>) JsonParserUtil.toPojo(serviceResponse.getStringResponse(),new TypeReference<List<ItemCategory>>(){});

任何指针将不胜感激。

提前致谢。

4

1 回答 1

7

您可能希望像这样注释您的 bean 类:

@JsonSerialize(
include=JsonSerialize.Inclusion.NON_NULL,
)

来源:JsonSerialize 注解 javadoc

于 2012-11-29T23:00:07.413 回答