使用Jackson将json转换为java对象

时间:2012-05-23 15:23:11

标签: java json jackson

基于这张json,需要什么Jackson POJO结构?

有点像? :

class POJO {

    private List<ToAddList> toAdd;
    private List<ToRemoveList> toRemove

}

class ToAddList(){
String name;
int pos;
}

class ToRemoveList(){
String name
}


///////////////////////JSON///////////////////////////
    {
        "toAdd": [
            {
                "name": "test",
                "pos": 0,
            },
            {
                "name": "test",
                "pos": 1,
            },
        ],
        "toRemove": [
            {
                "name": "test"
            },
            {
                "name": "test"
            }
        ]
    }

1 个答案:

答案 0 :(得分:2)

你有一个带有两个字段的简单bean,一个字符串和一个数字。这个bean用在列表中,列表包含在另一个bean中:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class SimpleBean implements Serializable {
    private String name;
    private Integer pos;

    // constructors, getters, setters
}

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class RequestBean implements Serializable {
    private List<SimpleBean> toAdd;
    private List<SimpleBean> toRemove;

    // constructors, getters, setters
}

多数民众赞成。