Jackson 解开数组中的对象

Ale*_*lex 5 java arrays json jackson

您好,我有一些 JSON,其包装如下:

{
  rootNode: [{
    "property":"value"
  }]
}
Run Code Online (Sandbox Code Playgroud)

有没有办法获取数组中的对象:

@JsonRootName("rootNode")
public class ThisClass{
  private String property;
}
Run Code Online (Sandbox Code Playgroud)

如果没有数组,我只能使用根节点表示法,是否有其他注释来补偿包装的数组?

Man*_*dis 6

您可以通过在 jackson 中启用以下反序列化选项来解析此 JSON:

ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
Run Code Online (Sandbox Code Playgroud)

检查文档了解详细信息