gpt4 book ai didi

json - MongoDB ObjectId的Jackson JSON反序列化

转载 作者:行者123 更新时间:2023-12-03 15:05:25 28 4
gpt4 key购买 nike

好的,所以首先这里是从我的Web服务返回的JSON。我试图在我的Android ContentProvider中的ResponseHandler中进行异步查询后将其反序列化为pojos。

{"exampleList" : [{
"locationId" : "00001" ,
"owners" : [
{
"paidID" : { "$oid" : "50a9c951300493f64fbffdb6"} ,
"userID" : { "$oid" : "50a9c951300493f64fbffdb6"}
} ,
{
"paidID" : { "$oid" : "50a9c951300493f64fbffdb7"} ,
"userID" : { "$oid" : "50a9c951300493f64fbffdb7"}
}
]
}]}

刚开始,我对看到的问题感到困惑,因为我在Web服务中使用的是与我的Android应用程序中相同的Jackson注释的bean,但后来我意识到 owners对象从未在示例中发送JSON到我的Web服务(它跳过我的Web服务上的POJO,并通过DAO的原子更新被添加到mongoDB的文档中)。

那么好吧。到目前为止,Jackson不必处理 owners对象,现在它正在使它窒息,即:

JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token at [char position where you can find "userID" and "paidID"] through reference chain [path to my Jackson bean which contains the owners class]



我的Jackson bean 有一个包装器,这就是“exampleList”的全部内容:
public class Examples extends HashMap<String, ArrayList<Example>> {

}

然后是实际的 Example类:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Example implements Comparable<Example> {

@ObjectId @Id
private String id;

@JsonProperty(Constants.Example.location)
private String location;

@JsonProperty(Constants.Example.OWNERS)
private List<Owners> owners;

public int compareTo(Example _o) {
return getId().compareTo(_o.getId());
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}
public List<Example.Owners> getOwners() {
return owners;
}

public void setOwners(List<Example.Owners> owners) {
this.owners = owners;
}

public Example() {
}

@JsonCreator
public Example(@Id @ObjectId String id) {
this.id = id;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Owners implements Comparable<Owners> {

@JsonProperty(Constants.Example.USERID)
private String userID;

@JsonProperty(Constants.Example.PAIDID)
private String paidID;

public Owners() {
}

public int compareTo(Owners _o) {
return getUserID().compareTo(_o.getUserID());
}

@ObjectId
public String getUserID() {
return userID;
}

@ObjectId
public void setUserID(String userID) {
this.userID = userID;
}

@ObjectId
public String getPaidID() {
return paidID;
}

@ObjectId
public void setPaidID(String paidID) {
this.paidID = paidID;
}

}

}

最后,这都是失败的ResponseHandler中的代码(第二行产生JsonMappingException):
objectMapper = MongoJacksonMapperModule.configure(objectMapper);    
mExamples = objectMapper.readValue(jsonParser, Examples.class);

我有一个问题,就是 jackson 仍然不知道如何映射mongoDB ObjectIds的 $oid。 MongoJacksonMapper库应该通过提供 @ObjectId批注和一种配置ObjectMapper以使用该库的方式来帮助实现此目的,但仍无法正常工作。出于某种原因,它仍在将userID或paidID作为字符串而不是ObjectId进行查找。有任何想法吗?

最佳答案

另一种选择是com.fasterxml.jackson.databind.ser.std.ToStringSerializer

@Id
@JsonSerialize(using = ToStringSerializer.class)
private final ObjectId id;

这将导致:
{
"id": "5489f420c8306b6ac8d33897"
}

关于json - MongoDB ObjectId的Jackson JSON反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14012890/

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