如何使用Jackson将JSON数组反序列化为java对象

dfty9e19  于 2022-11-08  发布在  Java
关注(0)|答案(1)|浏览(174)

我有一个JSON数组

{ "inList" : 
    [
        { "cmd" : "enqueue", "name" : "job1", "pri" : 4 }, 
        { "cmd" : "enqueue", "name" : "job2", "pri" : 3 },
        { "cmd" : "dequeue" },
        { "cmd" : "enqueue", "name" : "job3", "pri" : 0 },
        { "cmd" : "enqueue", "name" : "job4", "pri" : 1 },
        { "cmd" : "dequeue" }
    ]
}

我想使用强大的Jackson将JSON反序列化为java对象。
而我有命中类(我觉得它有什么不对劲)

public class InList {
    private String[] inList;
    private LinkedList<Object> jobs;

    public InList() { }

    public String[] getInList() {
        return inList;
    }

    public void setInList(String[] inList) {
        this.inList = inList;
    }

    public LinkedList<Object> getJobs() {
        return jobs;
    }

    public void setJobs(LinkedList<Object> jobs) {
        this.jobs = jobs;
    }
}

当我试图反序列化JSON时,它就是不能命中类

ObjectMapper mapper = new ObjectMapper();

InList inList = null;
try {
    inList = mapper.readValue(jsonStr, InList.class);
}

你能帮我弄明白吗?
谢谢你!

hyrbngr7

hyrbngr71#

您的Bean类应该如下所示:-
第一个

相关问题