我使用Jackson,ObjectMapper.readValue(json,Class)。
上课:
Component {
private String name;
private String someField;
private boolean show = true; // if false -> skip it object
}
和可扩展的类:
ExtendedComponent extends Component {
private List<Component> components = emptyList();// if all of object in list not showed -> skip field
}
和复杂类:
ComplexComponent extends Component {
ExtendedComponent component;
}
和json:
{
"complexComponent": {
"name": "complexName1",
"show": true,
"someField": "complex",
"extendedComponent": {
"components": [
{
"name": "someName1",
"show": true,
"someField": "someField"
},
{
"name": "someName2",
"show": false,
"someField": "someField"
},
{
"name": "someName3",
"show": true,
"someField": "someField"
}
]
}
}
}
如何在ExtendedComponent中只获取名称为someName1和someName3的对象?
如果没有显示所有组件,如何获得可以为空的ComplexComponent?
答案 0 :(得分:2)
取决于您的杰克逊2版本
@JsonInclude(Include.NON_DEFAULT)
或
@JsonInclude(Include.NON_EMPTY)
应该可以解决问题。