我正在尝试使用Jackson将JSON数组反序列化为Java Collection。这是我昨晚问到的这个问题的答案的动机Can I instantiate a superclass and have a particular subclass be instantiated based on the parameters supplied。
我得到的错误是(为了便于阅读而增加了换行符):
org.codehaus.jackson.map.JsonMappingException:
Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type'
that is to contain type id (for class sempedia.model.query.QueryValue)
at [Source: java.io.StringReader@325aef; line: 1, column: 175]
(through reference chain: sempedia.model.query.QueryProperty["values"])
我的情况非常复杂。我的数组包含的对象本身包含一个数组值。该数组又包含也是对象但不一定相同的值(因此是多态性)。
以下是一个示例JSON字符串:
[
{
"id":"74562",
"uri":"http://dbpedia.org/ontology/family",
"name":"family",
"values":[
{
"id":"74563",
"uri":"http://dbpedia.org/resource/Orycteropodidae",
"name":"Orycteropodidae"
}
],
"selected":false
},
{
"id":"78564",
"uri":"http://dbpedia.org/ontology/someNumber",
"name":"someNumber",
"values":[
{
"lower":"45",
"upper":"975",
}
],
"selected":true
}
]
我想使用这个(下面)代码或类似的东西来获取一个对象,它是Collection<QueryProperty>
的一个实例,我称之为queryProperties
ObjectMapper mapper = new ObjectMapper();
Collection<QueryProperty> queryProperties =
queryProperties = mapper.readValue(query,
new TypeReference<Collection<QueryProperty>>(){});
我的反序列化课程(他们有我不打印的公共getter / setter)如下所示:
public class QueryProperty {
int id;
String uri;
String name;
Set<QueryValue> values;
String selected;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@Type(value = ResourceQueryValue.class),
@Type(value = NumericQueryValue.class)
})
public abstract class QueryValue {
String type;
}
ResourceQueryValue
public class ResourceQueryValue extends QueryValue{
int id;
String uri;
String name;
}
NumericQueryValue
相同的JSON不包含此类型的对象。
public class NumericQueryValue extends QueryValue{
double lower;
double upper;
}
堆栈跟踪的初始部分:
org.codehaus.jackson.map.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type' that is to contain type id (for class sempedia.model.query.QueryValue)
at [Source: java.io.StringReader@325aef; line: 1, column: 175] (through reference chain: sempedia.model.query.QueryProperty["values"])
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.wrongTokenException(StdDeserializationContext.java:240)
at org.codehaus.jackson.map.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:86)
at org.codehaus.jackson.map.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:89)
答案 0 :(得分:15)
正如经常发生的那样,写出问题有助于您了解解决方案。所以我需要做两件事。
首先,我需要将类型信息添加到JSON中 - 这不是我真正想要做的,但我想你需要在某处提供这些信息。
然后我需要将QueryValue上的注释编辑为:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@Type(value = ResourceQueryValue.class, name = "ResourceQueryValue"),
@Type(value = NumericQueryValue.class, name= "NumericQueryValue")
})