次のようなJSON構造があるとします。
{
"field1": "val1",
"field2": "v2",
"f3": "v3",
"f4": "v4",
"arrayOfStuff": [
{
"f5": "v5",
....
"f10": "v10"
}
],
"attributes": [
{"att1": "att1"},
{"att2": "attr2"},
{"att3": "att3"}
],
"options": [
"ignoreMismatchFile"
]
}
私たちの一致するJavaクラスは次のようになります:
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;
....
}
解析コードは次のようになります。
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;
}
私たちはこれを達成するためのさまざまな方法を試みましたが、もともとprivate List<MessageAttribute> attributes;
として属性を持つことを試みましたが、どちらもうまくいきませんでした(別の答え - 動作しません)
私たちの目標は、属性のハードコードされた属性のリストではなく、属性を動的に保つことです。
これは、MessageAttribute
クラスが次のようになっている方法です。
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;
}
}
現在取得している例外は次のとおりです。
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"])