3

ジャクソンを使用して、ジェイソンの応答をポジョのリストに変換しています。以下は私が得ている応答です。

[

    {
        "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;
}
}

すべてが正常に動作します。しかし、空白/null値としてコードを持つ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;

以下のコードを使用して、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 に答える