Como mapear dinamicamente matriz JSON de valores fundamentais para a sub-objeto usando Jackson?

Carmageddon:

Digamos que temos uma estrutura JSON parecida com esta:

{
 "field1": "val1",
 "field2": "v2",
 "f3": "v3",
 "f4": "v4",
 "arrayOfStuff": [
  {
   "f5": "v5",
   ....
   "f10": "v10"
  }
 ],
 "attributes": [
  {"att1": "att1"},
  {"att2": "attr2"},
  {"att3": "att3"}
 ],
 "options": [
  "ignoreMismatchFile"
 ]
}

E nossos olhares classe java harmonização como:

public class Message {
   @IsUniqueId
   private String field1; //
   private String field2;
   private String field3;
   private String field4;
   private List<AnotherObject> f5;
   @JsonProperty("attributes")
   private LinkedHashMap<String, String> attributes;
   private List<String> options;
   ....
}

Os olhares código de análise gosto:

protected Message loadSavedMessageAsMessageObject(String path) throws IOException {
    File file = ResourceUtils.getFile(path);
    if (file.exists()) {
        ObjectMapper mapper = this.getObjectMapper();
        return mapper.readValue(file, Message.class);
    }

    return null;
}

Nós tentamos diferentes maneiras de realizar isso, originalmente nós tentamos ter atributos como private List<MessageAttribute> attributes;, mas que não funcionou ou (nós mudamos para o mapa baseado em Outra resposta - não trabalho)

Nosso objetivo é manter os atributos de um dinâmico, não lista codificado de atributos.

Isto é como a MessageAttributeclasse parecia:

public class MessageAttribute {
    private String key;
    private String value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

A exceção que atualmente recebe é:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.LinkedHashMap` out of START_OBJECT token
at [Source: (File); line: 32, column: 3] (through reference chain: com.org.Message["attributes"])
Piscina morta :

Correspondente MessagePOJO acima JSON está no formato errado, eu fiz algumas mudanças attributesdeve ser List of Mape lista de AnotherObjectdeve apontar paraarrayOfStuff

public class Message {
 @IsUniqueId
 private String field1; //
 private String field2;
 private String field3;
 private String field4;
 private List<AnotherObject> arrayOfStuff;  //or you can have List<Map<String,String>> arrayOfStuff
 @JsonProperty("attributes")
 private List<LinkedHashMap<String, String>> attributes; // this is list of map objects
 private List<String> options;
  ....
  }

Acho que você gosta

Origin http://43.154.161.224:23101/article/api/json?id=181485&siteId=1
Recomendado
Clasificación