java - JSON 到 POJO - 忽略某些嵌套属性
问题描述
假设我有一个看起来像这样的 JSON 对象
{
"name":"John",
"age":30,
"someAttribute1": {
"property1":"example1",
"property2":"example2"
},
"someAttribute2": {
"property1":"example1",
"property2":"example2"
}
}
以及将该实体读入的以下 POJO 类
@XmlRootElement
public class Person {
@XmlElement(name = "name")
private String name;
@XmlElement(name = "age")
private int age;
}
如何获得和 的property1
字段,而不必为and创建单独的类表示?someAttribute1
property1
someAttribute2
somAttribute1
someAttribute2
解决方案
您执行此操作的方式是Map<KeyType, ValueType>
例如在您的情况下使用 a Map<String, String>
will 来完成工作。代码应该这样工作:
@XmlRootElement
public class Person {
@XmlElement(name = "name")
private String name;
@XmlElement(name = "age")
private int age;
@XmlElement(name = "someAttribute2")
private Map<String, String> someAttributeTwo;
@XmlElement(name = "someAttribute1")
private Map<String, String> someAttributeOne;
}
推荐阅读
- amazon-web-services - 为什么 AWS Elastic Beanstalk 不通过自定义 JWT?
- r - 用同一列中的多个字符串替换多个字符串?
- arrays - 用双引号解析 Ruby 字符串
- r - 如何仅将字符串的一部分处理为R中的日期
- wordpress - 配置 WP Rocket,高级
- php - 发送 PHPMailer 电子邮件后删除“附件”文件夹
- java - Junit 测试 objectMapper 读取值
- firebase - Firebase 模拟器简短错误信息
- python - 节拍:连接错误:从套接字读取时出错:(104,“对等连接重置”)
- visual-studio-code - VS Code 在启动时崩溃