[英]How can I deserialize a JSON array of “name” “value” pairs to a Pojo using Jackson
I want to deserialize the following JSON object: 我想反序列化以下JSON对象:
{
"id":"001",
"module_name":"Users",
"name_value_list":
{
"user_name": {"name":"user_name", "value":"admin"},
"full_name": {"name":"full_name", "value":"Lluís Pi"},
"city": {"name":"full_name", "value":"Barcelona"},
"postal_code": {"name":"postal_code", "value":"08017"},
...
}
}
into some Java object like this: 变成这样的一些Java对象:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE)
public class UserEntry
{
private String id;
private String moduleName;
private Person nameValueList;
public String getId()
{
return id;
}
public String getModuleName()
{
return moduleName;
}
public Person getPerson()
{
return nameValueList;
}
}
where Person is the following class: 其中Person是以下类别:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE)
class Person
{
private String userName;
private String fullName;
private String city;
private String postalCode;
}
using Jackson but I get a deserialization error. 使用杰克逊,但我得到反序列化错误。
If I change the type of field nameValueList
to a Map all the deserialization process goes with no problem and I get a map where the key is the "name" value and the value is the "value" value. 如果我将字段
nameValueList
的类型更改为Map,则所有反序列化过程都没有问题,并且得到了一个映射,其中键是“名称”值,而值是“值”值。
So my question is: is there any simple, or no so simple, way to deserialize this kind of JSON object to a Java Pojo with properties prop_1
, prop_2
, prop_3
and prop_4
? 所以我的问题是:有没有简单的方法,或者没有这么简单的方法,可以将这种JSON对象反序列化为具有属性
prop_1
, prop_2
, prop_3
和prop_4
的Java Pojo?
{
"name_value_list":
{
"prop_1": {"name":"prop_1", "value":"value_1"},
"prop_2": {"name":"prop_2", "value":"value_2"},
"prop_3": {"name":"prop_3", "value":"value_3"},
"prop_4": {"name":"prop_4", "value":"value_4"},
...
}
}
Not very simple and not very clean. 不是很简单,也不是很干净。 However you can do it by implementing a any setter field for the JSON attributes in the Person class which don't match any attribute on your UserEntry POJO.
但是,您可以通过为Person类中的JSON属性实现一个与 setUser字段不匹配的UserEntry POJO属性来实现此目的。
@JsonAnySetter
public void putUserField(String userKey, Map<String, String> userValue)
throws NoSuchFieldException {
String actualFieldName = getActualFieldName(userKey);
Field field = this.getClass().getDeclaredField(actualFieldName);
field.setAccessible(true);
ReflectionUtils.setField(field, this, userValue.get("value"));
}
private String getActualFieldName(String userKey) {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, userKey);
}
In addition to that, I had to change the Jackson attributes for the Person class to 除此之外,我还必须将Person类的Jackson属性更改为
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
getterVisibility = JsonAutoDetect.Visibility.NONE)
for it to work for attributes like "city" which don't need any name transformation because jackson tries to directly set the field which fails. 它可以用于诸如“ city”之类的属性,这些属性不需要任何名称转换,因为jackson尝试直接设置失败的字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.