java - 使用 Jackson json 跳过一层抽象

标签 java json jackson

我有这样的类(class)

public class Profile {
  Profile profile
  Interests interests
}


public class Interests {
  HashMap<String,InterestMetric> interests = ...
}

如果我尝试使用 jackson 将其序列化为 json:

{
  profile:...
  interests: 
    {
       interests:...
    }
}

有没有办法避免在没有自定义序列化器(注释)的情况下创建 2 个兴趣标签?

最佳答案

是的:

public class Profile {
  Profile profile;
  HashMap<String,InterestMetric> interests = ...
}

如果第二个类只包含 map ,为什么还需要它?

再想一想...为什么 Profile 包含 Profile?它不应该只包含配置文件中汇总的信息吗?

编辑: 既然您提到代码库不会改变, @JsonUnwrapped annotation应该是您正在寻找的:

public class Profile {
  Profile profile
  @JsonUnwrapped
  Interests interests
}


public class Interests {
  HashMap<String,InterestMetric> interests = ...
}

我无法从我所在的位置测试它,但它应该将您的输出更改为:

{
  profile:...
  interests:...
}

关于java - 使用 Jackson json 跳过一层抽象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21380634/

相关文章:

java - 滑动刷新后加载更多不起作用

java - 从 Java 运行 Oracle 查询

android - GSON 映射键值对

java - Jackson:反序列化 ArrayList 不起作用?

java - Jackson 的 @JSONView 的 Scala 等效代码

json - spring prefixjson 与 responsebody

java - 使用实例变量 java 的多线程

java - 在Java 11中使用HttpBuilder API,在哪里指定hostNameVerifier?

json - Jenkins 更新后,readJSON 和 readYaml 在 Jenkins 中停止工作

javascript - 将 JSON 文件作为变量加载到 JS