如何使用 Jackson 将 JSON 键值数组动态映射到子对象?
How to dynamically map JSON array of key values to sub-object using Jackson?
假设我们有一个 JSON 结构,如下所示:
{
"field1": "val1",
"field2": "v2",
"f3": "v3",
"f4": "v4",
"arrayOfStuff": [
{
"f5": "v5",
....
"f10": "v10"
}
],
"attributes": [
{"att1": "att1"},
{"att2": "attr2"},
{"att3": "att3"}
],
"options": [
"ignoreMismatchFile"
]
}
我们的匹配 java class 看起来像:
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;
,但这也没有用(我们切换到基于 Another answer 的地图 - 不起作用)
我们的目标是让属性保持动态,而不是硬编码的属性列表。
这就是 MessageAttribute
class 的样子:
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"])
上面 JSON 对应的 Message
POJO 格式错误,我做了一些更改 attributes
应该是 List of Map
并且列表 AnotherObject
应该指向
arrayOfStuff
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;
....
}
假设我们有一个 JSON 结构,如下所示:
{
"field1": "val1",
"field2": "v2",
"f3": "v3",
"f4": "v4",
"arrayOfStuff": [
{
"f5": "v5",
....
"f10": "v10"
}
],
"attributes": [
{"att1": "att1"},
{"att2": "attr2"},
{"att3": "att3"}
],
"options": [
"ignoreMismatchFile"
]
}
我们的匹配 java class 看起来像:
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;
,但这也没有用(我们切换到基于 Another answer 的地图 - 不起作用)
我们的目标是让属性保持动态,而不是硬编码的属性列表。
这就是 MessageAttribute
class 的样子:
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"])
上面 JSON 对应的 Message
POJO 格式错误,我做了一些更改 attributes
应该是 List of Map
并且列表 AnotherObject
应该指向
arrayOfStuff
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;
....
}