关于使用 Jackson 展平嵌套 json 的查询
Query about flattening a nested json using Jackson
我有一个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;
}
}
我知道我总是可以为子对象创建另一个 pojo,但我想知道有没有办法将嵌套的子属性直接映射到 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");
}
}
我有一个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;
}
}
我知道我总是可以为子对象创建另一个 pojo,但我想知道有没有办法将嵌套的子属性直接映射到 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");
}
}