JSON 到 POJO - 忽略某些嵌套属性
JSON to POJO - Ignore certain nested properties
假设我有一个 JSON 看起来像这样的对象
{
"name":"John",
"age":30,
"someAttribute1": {
"property1":"example1",
"property2":"example2"
},
"someAttribute2": {
"property1":"example1",
"property2":"example2"
}
}
以及以下 POJO class 将该实体读入
@XmlRootElement
public class Person {
@XmlElement(name = "name")
private String name;
@XmlElement(name = "age")
private int age;
}
如何获得 someAttribute1
的 property1
字段和 someAttribute2
的 property1
字段,而不必为 class 创建单独的表示somAttribute1
和 someAttribute2
?
从你的例子来看,似乎适合你的情况是在你的 Person class 中添加两个 hashmap 成员变量,如下所示:
@XmlRootElement
public class Person {
@XmlElement(name = "name")
private String name;
@XmlElement(name = "age")
private int age;
}
@XmlElement(name = "someAttribute1")
private HashMap<String,String> someAttribute1;
}
@XmlElement(name = "someAttribute2")
private HashMap<String,String> someAttribute2;
}
你这样做的方法是使用 Map<KeyType, ValueType>
例如在你的情况下 Map<String, String>
将完成工作。代码应该像这样工作:
@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;
}
您可以使用 Jackson 库解压嵌套的 属性,方法是结合使用 @JsonProperty
和 class 中的一些自定义逻辑。
public class Person {
private String name;
private int age;
private String someAttribute1Property1;
}
@SuppressWarnings("unchecked")
@JsonProperty("someAttribute1")
private void unpackNested(Map<String,Object> someAttribute1) {
this.someAttribute1= (String)brand.get("Property1");
-------
}
}
假设我有一个 JSON 看起来像这样的对象
{
"name":"John",
"age":30,
"someAttribute1": {
"property1":"example1",
"property2":"example2"
},
"someAttribute2": {
"property1":"example1",
"property2":"example2"
}
}
以及以下 POJO class 将该实体读入
@XmlRootElement
public class Person {
@XmlElement(name = "name")
private String name;
@XmlElement(name = "age")
private int age;
}
如何获得 someAttribute1
的 property1
字段和 someAttribute2
的 property1
字段,而不必为 class 创建单独的表示somAttribute1
和 someAttribute2
?
从你的例子来看,似乎适合你的情况是在你的 Person class 中添加两个 hashmap 成员变量,如下所示:
@XmlRootElement
public class Person {
@XmlElement(name = "name")
private String name;
@XmlElement(name = "age")
private int age;
}
@XmlElement(name = "someAttribute1")
private HashMap<String,String> someAttribute1;
}
@XmlElement(name = "someAttribute2")
private HashMap<String,String> someAttribute2;
}
你这样做的方法是使用 Map<KeyType, ValueType>
例如在你的情况下 Map<String, String>
将完成工作。代码应该像这样工作:
@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;
}
您可以使用 Jackson 库解压嵌套的 属性,方法是结合使用 @JsonProperty
和 class 中的一些自定义逻辑。
public class Person {
private String name;
private int age;
private String someAttribute1Property1;
}
@SuppressWarnings("unchecked")
@JsonProperty("someAttribute1")
private void unpackNested(Map<String,Object> someAttribute1) {
this.someAttribute1= (String)brand.get("Property1");
-------
}
}