gpt4 book ai didi

json - 如何反序列化 java pojo 类中的 json 对象?

转载 作者:行者123 更新时间:2023-12-04 15:59:48 25 4
gpt4 key购买 nike

我有一个简单的 JSON 语句,它的类型非常符合需求。像这样

 {
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 类代码已给出
@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 时,它给出了以下异常(exception)

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



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

最佳答案

尽量只留下 @JsonProperty("mbox") ArrayList<String> listMbox;字段(不需要 @JsonProperty("mbox")
String mbox;
)
并添加 Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY=true到 Jackson 对象映射器配置。

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

关于json - 如何反序列化 java pojo 类中的 json 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13740901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com