小编car*_*los的帖子

jackson desearlization:根有两个键。我如何打开一个并忽略另一个?

使用杰克逊 2.x

json 响应如下所示:

{
 "flag": true,
 "important": {
   "id": 123,
   "email": "foo@foo.com"
 }
}
Run Code Online (Sandbox Code Playgroud)

“flag”键不提供任何有用的信息。我想忽略“标志”键并将“重要”值解包到重要的实例。

public class Important {

    private Integer id;
    private String email;

    public Important(@JsonProperty("id") Integer id,
                     @JsonProperty("email") String email) {
        this.id = id;
        this.email = email;
    }

    public String getEmail() { this.email }

    public Integer getId() { this.id }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试将 @JsonRootName("important") 添加到重要并使用 DeserializationFeature.UNWRAP_ROOT_VALUE 配置 ObjectMapper 时,我收到一个 JsonMappingException:

根名称“标志”与类型的预期(“重要”)不匹配...

当我从 JSON 中删除“标志”键/值时,数据绑定工作得很好。如果我也将 @JsonIgnoreProperties("flag") 添加到重要,我会得到相同的结果。

更新


更新的类......实际上会通过编译步骤

@JsonRootName("important")
public static class Important {
    private Integer id;
    private …
Run Code Online (Sandbox Code Playgroud)

java json jackson

5
推荐指数
1
解决办法
2196
查看次数

标签 统计

jackson ×1

java ×1

json ×1