[英]Jackson deserialize generic with unknown property name
I have following JSON
. 我有以下
JSON
。 And I am parsing it using Jackson
Parser 我正在使用
Jackson
Parser进行解析
{
"code": 0,
"response": {
"pagination": {
"page": 1,
"limit": 20,
"count": 5,
"pageCount": 1
},
"random": [
....
]
}
}
Now I have simple POJO
classes created for various random
object. 现在,我已经为各种
random
对象创建了简单的POJO
类。 I expect 3-4 different types of random
object. 我希望有3-4种不同类型的
random
对象。 So instead of creating different wrapper classes for different types of 'random' object I created a generic one 因此,我没有为不同类型的“随机”对象创建不同的包装器类,而是创建了一个通用类
EDITED CLASS:
编辑课程:
public class PaginatedResponse<E> {
private Pagination pagination;
private List<E> responseList;
public Pagination getPagination() {
return pagination;
}
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
public List<E> getResponseList() {
return responseList;
}
public void setResponseList(List<E> responseList) {
this.responseList = responseList;
}
}
Now For mapping it I used, 现在我用来映射
JsonNode tree = mapper.readTree(response);
TypeReference<PaginatedResponse<LocationParent>> ref = new TypeReference<PaginatedResponse<LocationParent>>() { };
PaginatedResponse<LocationParent> resp = mapper.convertValue(tree.get("response"), ref);
But i am not able to map responseList
. 但是我无法映射
responseList
。 I get the pagination
object but the responseList
is always null. 我得到了
pagination
对象,但responseList
始终为null。 Now how to dynamically provide property name
for responseList
. 现在如何动态地为
responseList
提供property name
。
Please help 请帮忙
What you need for variable value type is handling for polymorphic types. 对于变量值类型,您需要处理多态类型。 Generic types alone won't help, since deserialization side would not know what type to use.
仅泛型类型无济于事,因为反序列化方面不知道要使用哪种类型。
You can enable polymorphic type handling with annotation @JsonTypeInfo
; 您可以使用注解
@JsonTypeInfo
启用多态类型处理; but a problem in this particular case is that you want a List
of things of arbitrary type -- due to type-erasure, all Java Lists are really just List<Object>
; 但是在这种特殊情况下的一个问题是您想要一个任意类型的事物的
List
-由于类型擦除,所有Java列表实际上只是List<Object>
; there is no typing for elements. 没有元素的输入。
If it was me, I would probably sub-class PaginatedResponse
and just add @JsonTypeInfo
in base class, like: 如果是我,我可能会子类
PaginatedResponse
,只是添加@JsonTypeInfo
在基类,如:
@JsonTypeInfo(...) // see javadocs for properties needed
public abstract class PaginatedResponse<T> {
public Pagination pagination;
// .. and so on
}
public class PaginatedFooResponse<Foo> { }
The reason to use sub-classing here is simply make it possible for deserializer to figure out element type, given type of response object. 在这里使用子类的原因仅仅是为了使反序列化器能够在给定响应对象类型的情况下找出元素类型。 Response object will have type (
PaginatedFooResposne
), and from that type of elements is available. 响应对象的类型为(
PaginatedFooResposne
),并且可以使用该类型的元素。
Try this:: 尝试这个::
JSONObject objJSON = JSONObject.fromString("urString");
String code = objJSON.get("code");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.