我有一个json
{
"parentId": "123",
"parentName": "abc",
"child": {
"childId": "456",
"childName": "xyz",
}
}
我的要求是将json展平,以便可以将其映射到pojo
class MyJson{
private String parentId;
private String parentName;
private String childId;
@JsonCreator
public MyJson(@JsonProperty("parentId"),@JsonProperty("parentName"),
@JsonProperty("childId")){
this.parentId = parentId;
this.parentName = parentName;
this.childId = childId;
}
}
我知道我总是可以为Child Object创建另一个pojo,但是我想知道是否有一种方法可以直接将嵌套的child属性映射到pojo?
child
是一个JSON Object
,您需要将其声明为Map
:
class MyJson {
private String parentId;
private String parentName;
private String childId;
@JsonCreator
public MyJson(@JsonProperty("parentId") String parentId,
@JsonProperty("parentName") String parentName,
@JsonProperty("child") Map<String, String> child) {
this.parentId = parentId;
this.parentName = parentName;
this.childId = child.get("childId");
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句