简体   繁体   English

用杰克逊1.9解析json

[英]Issue parsing json with jackson 1.9

I used Gson before and everything worked fine, but it is too slow. 我以前使用过Gson,但一切正常,但是速度太慢。

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 . 问题是,它不填充类items parx,pary,parz etc parx,pary,parz等

items is the only problem. items是唯一的问题。 the rest works fine. 其余的工作正常。

my structure has to be right because in Gson I only need two lines and it works perfectly. 我的结构必须正确,因为在Gson中,我只需要两行,并且效果很好。

so I think I have to add something so that jackson recognizes 所以我认为我必须添加一些东西,杰克逊才能认出

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 Gson使用Jackson ArrayList时使用LinkedLIst ,因此代码中的Gson无法转换Passes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM