0

I have a JSON array

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

I would like to use the powerful jackson to de serialize the JSON into java object.

And I have the hit class (I think it has something wrong)

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

And when I try to de serialize the JSON, it just can not hit the class

ObjectMapper mapper = new ObjectMapper();

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

Could you help me figure out?

Thanks!

3
  • 1
    Your json describes a field inList that contains an array of objects. The class has and inList field that is a list of String. Commented Oct 2, 2017 at 22:19
  • What would you expect the resulting object to look like? Commented Oct 2, 2017 at 22:31
  • You actually have a JSON object. The array is inside Commented Oct 3, 2017 at 0:03

1 Answer 1

1

Your bean class should be like below:-

import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;

public class InList {   
    @JsonProperty("inList")
    private List<InListDetail> inList;
    public List<InListDetail> getInList() {
        return inList;
    }
    public void setInList(List<InListDetail> inList) {
        this.inList = inList;
    }
}
import org.codehaus.jackson.annotate.JsonProperty;

public class InListDetail { 
    @JsonProperty("cmd")
    private String cmd;
    @JsonProperty("name")
    private String name;
    @JsonProperty("pri")
    private int pri;
    public String getCmd() {        
           return cmd;
    }
    public void setCmd(String cmd) {
        this.cmd = cmd;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public int getPri() {
        return pri;
    }

    public void setPri(int pri) {
        this.pri = pri;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.