如何在java pojo类中解除json对象的deseralize? [英] How can I deseralize json object in java pojo class?

查看:93
本文介绍了如何在java pojo类中解除json对象的deseralize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的JSON语句,该类型非常需要。喜欢这个

I have a simple JSON statement which type is very per need. like this

 {
       actor:{name:"kumar",mbox:"kumar@gmail.com"}
       verb :"completed"
    }

{
       actor:{name:["kumar","manish"],mbox:["kumar@gmail.com","manish@gmail.com"]}
       verb :{
            "id" : "http://adlnet.gov/expapi/verbs/completed",
        "display" : {
            "en-US" : "completed"
        }
    }

我正在使用POJO类来映射这个json字符串,并且pojo类代码被赋予了bleow

I am using using POJO class to map this json string and pojo class code is given bleow

@JsonProperty("actor")
Actor actor;
@JsonProperty("verb")
Verb objVerb;
@JsonProperty("verb")
String verb;
public Actor getActor() {
    return actor;
}
public void setActor(Actor actor) {
    this.actor = actor;
}
public Verb getObjVerb() {
    return objVerb;
}
public void setObjVerb(Verb objVerb) {
    this.objVerb = objVerb;
}
@JsonIgnore
public String getVerb() {
    return verb;
}
@JsonIgnore
public void setVerb(String verb) {
    this.verb = verb;
}
public static class Actor {
    String objectType;
    @JsonProperty("name")
    ArrayList<String> listName;
    @JsonProperty("name")
    String name;
    @JsonProperty("mbox")
    ArrayList<String> listMbox;
    @JsonProperty("mbox")
    String mbox;
    @JsonProperty("mbox_sha1sum")
    ArrayList<String> Listmbox_sha1sum;
    @JsonProperty("mbox_sha1sum")
    String mbox_sha1sum;
    @JsonProperty("openid")
    String openid;
    @JsonProperty("account")
    Account account;
    public String getObjectType() {
        return objectType;
    }

    public void setObjectType(String objectType) {
        this.objectType = objectType;
    }

    public ArrayList<String> getListName() {
        return listName;
    }

    public void setListName(ArrayList<String> listName) {
        this.listName = listName;
    }
    @JsonIgnore
    public String getName() {
        return name;
    }
    @JsonIgnore
    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<String> getListMbox() {
        return listMbox;
    }

    public void setListMbox(ArrayList<String> listMbox) {
        this.listMbox = listMbox;
    }
    @JsonIgnore
    public String getMbox() {
        return mbox;
    }
    @JsonIgnore
    public void setMbox(String mbox) {
        this.mbox = mbox;
    }

    public ArrayList<String> getListmbox_sha1sum() {
        return Listmbox_sha1sum;
    }

    public void setListmbox_sha1sum(ArrayList<String> listmbox_sha1sum) {
        Listmbox_sha1sum = listmbox_sha1sum;
    }
    @JsonIgnore
    public String getMbox_sha1sum() {
        return mbox_sha1sum;
    }
    @JsonIgnore
    public void setMbox_sha1sum(String mbox_sha1sum) {
        this.mbox_sha1sum = mbox_sha1sum;
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public static class Account {
        @JsonProperty("homePage")
        String homePage;
        @JsonProperty("name")
        String name;
        public String getHomePage() {
            return homePage;
        }
        public void setHomePage(String homePage) {
            this.homePage = homePage;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
}
public static class Verb {
    String id;
    Map<String,String> display;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public Map<String, String> getDisplay() {
        return display;
    }
    public void setDisplay(Map<String, String> display) {
        this.display = display;
    }
}

我正在使用jaxb和jakson。我正在实现webservice来处理json语句
所以我使用bean类来映射json。但是当我用来映射这个json时,它会给出以下异常

I am using jaxb and jakson. I am implementing the webservice to handle the json statement so I use the bean class to map with json. But when I use to map this json then it gives the following exceptions


org.codehaus.jackson.map.JsonMappingException:带有名称的属性mbox有两个条目。

org.codehaus.jackson.map.JsonMappingException : property with the name "mbox" have two entry.

定义一个合适的bean结构,使其直接映射到beans类

Define a proper bean structure so it directly mapped to the beans class

推荐答案

尝试只留下 @JsonProperty(mbox)ArrayList< String> listMbox; 字段(不需要 @JsonProperty(mbox)
String mbox;

并添加 Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY = true 到Jackson对象映射器配置。

Try to leave only @JsonProperty("mbox") ArrayList<String> listMbox; field (don't need @JsonProperty("mbox") String mbox;) and add Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY=true to Jackson object mapper config.

因此在反序列化中它将能够同时获得两个数组和单个元素。

So in deserialization it will be able to get as both array and single element.

这篇关于如何在java pojo类中解除json对象的deseralize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆