user2848916
user2848916

Reputation: 13

Issue parsing json with jackson 1.9

I used Gson before and everything worked fine, but it is too slow.

Here is the Json:

"{"Info":[{"par1":3456,"par2":4500,"par3":0,"items":{"parx":2354,"pary":456456,"parz":"worker"}}
    ,{"par1":34456,"par2":4300,"par3":1,"items":{"parx":5677,"pary":78456,"parz":"member"}},
    ],"par4":343434,"duplicateItemIdList":null,"errorState":null}"

Now I tried Jackson:

code snippets:

ObjectMapper mapper = new ObjectMapper();
Passes mj = mapper.readValue(str, Passes.class);

public class Passes {
    public ArrayList<Info> info;   

.... }

@JsonIgnoreProperties(ignoreUnknown = true)    
class Info {

        public String par1 = "";
        public String par2 = "";
        public String par3 = "";
        public String par4 = "";   

        Items items = new Items();    
}

class Items{           
        public String parx = "";
        public String pary = "";
        public String parz = "";
}

the Problem is it doesn't fill the class items. parx,pary,parz etc

items is the only problem. the rest works fine.

my structure has to be right because in Gson I only need two lines and it works perfectly.

so I think I have to add something so that jackson recognizes

Upvotes: 1

Views: 147

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

Be sure that you have:

  • geters/setters for each parameter
  • empty (or any ) Contractor

Passes

@JsonIgnoreProperties(ignoreUnknown = true)
public class Passes {
    private List<Info> info;  

    public Passes() {
        // TODO Auto-generated constructor stub
    }

    public List<Info> getInfo() {
        return info;
    }

    public void setInfo(List<Info> info) {
        this.info = info;
    }
}

Info

@JsonIgnoreProperties(ignoreUnknown = true)    
public  class Info {

    private int par1;
    private int par2;
    private int par3;
    private int par4;

    //private Items items;    

    public Info() {
        // TODO Auto-generated constructor stub
    }

    public int getPar1() {
        return par1;
    }

    public void setPar1(int par1) {
        this.par1 = par1;
    }

    public int getPar2() {
        return par2;
    }

    public void setPar2(int par2) {
        this.par2 = par2;
    }

    public int getPar3() {
        return par3;
    }

    public void setPar3(int par3) {
        this.par3 = par3;
    }

    public int getPar4() {
        return par4;
    }

    public void setPar4(int par4) {
        this.par4 = par4;
    }
}

Items

public class Items{           
    private int parx;
    private int pary;
    private String parz;

    public Items() {
        // TODO Auto-generated constructor stub
    }

    public int getParx() {
        return parx;
    }

    public void setParx(int parx) {
        this.parx = parx;
    }

    public int getPary() {
        return pary;
    }

    public void setPary(int pary) {
        this.pary = pary;
    }

    public String getParz() {
        return parz;
    }

    public void setParz(String parz) {
        this.parz = parz;
    }
}

It should work

As a side note:

Gson uses LinkedLIst when Jackson ArrayList therefore from your code Gson fail to convert Passes class

Upvotes: 1

Related Questions