简体   繁体   English

如何使用Jackson将“名称”“值”对的JSON数组反序列化为Pojo

[英]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_1prop_2prop_3prop_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.

相关问题 如何使用 Jackson 将此动态键 JSON 反序列化为 Java 自定义 Pojo - How to deserialize this dynamic keys JSON to Java custom Pojo using Jackson 无法将JSON反序列化为POJO(使用Jackson) - Unable to deserialize JSON into a POJO (using Jackson) 使用Jackson反序列化从JSON到POJO的消息 - Deserialize message from JSON to POJO using Jackson 将 JSON 反序列化为 ArrayList<POJO> 使用杰克逊 - Deserialize JSON to ArrayList<POJO> using Jackson 如何使用Jackson JSON反序列化枚举列表? - How can I deserialize a list of enums using Jackson JSON? 如何使用Jackson来反序列化此JSON? - How do I deserialize this JSON using Jackson? JACKSON:如何在使用 Jackson 将 POJO 转换为 JSON 时忽略 POJO 名称? - JACKSON: How to ignore the POJO name while converting a POJO to JSON using Jackson? 将JSON对象的名称/值对反序列化为数组的元素 - Deserialize JSON object name-value pairs as elements of an array Codehaus Jackson JSON数据转换为POJO无法反序列化实例...超出START_ARRAY令牌 - Codehaus Jackson JSON data to POJO Can not deserialize instance … out of START_ARRAY token 如何在 jackson 中反序列化 json 数组和普通属性到 pojo。 数组单独工作 - How to deserialize a json array followed by a normal property into a pojo in jackson. The array alone works
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM