我想这可能早些时候曾被问过,但找不到我想要的东西。所以就这样了。
我的JSON看起来像这样 -
{
"verticals": [
{
"name": "vertical1",
"icon": "icon1",
"children": []
},
{
"name": "vertical2",
"icon": "icon2",
"children": []
}
],
"config": {
"vertical1": [
{
"title": "Section1",
"icon": "icon1",
"prefs": [
{
"type": "type1",
"opts": [
{
"name": "pref_store_key",
"value": "filter-prefix-food"
}
],
"icons": [
"icon3",
"icon4"
]
},
{
"type": "type2",
"opts": [
{
"name": "pref_store_key",
"value": "filter-prefix-cue"
}
],
"icons": [
"icon5",
"icon6"
]
}
]
}
],
"vertical2": [
{
"title": "Section1",
"icon": "icon1",
"prefs": [
{
"type": "type1",
"opts": [
{
"name": "pref_store_key",
"value": "filter-prefix-food"
}
],
"icons": [
"icon3",
"icon4"
]
},
{
"type": "type2",
"opts": [
{
"name": "pref_store_key",
"value": "filter-prefix-cue"
}
],
"icons": [
"icon5",
"icon6"
]
}
]
}
]
}
}
我的课程是 -
public class FeatureConfigData {
private Map<String, List<ItemConfig>> config = null;
private List<Vertical> verticals = null;
public List<ItemConfig> getItemConfig(String vertical) {
if (config == null) {
return null;
} else {
return config.get(vertical);
}
}
public List<Vertical> getVerticals() {
return verticals;
}
public Map<String, List<ItemConfig>> getConfig() {
return config;
}
public void setConfig(Map<String, List<ItemConfig>> config) {
this.config = config;
}
}
public class ItemConfig {
private String title = null;
private String icon = null;
private List<Pref> prefs = null;
public static class Pref {
private String type = null;
private List<String> icons = null;
private List<Opt> opts = null;
public String getType() { return type; }
public List<String> getIcons() { return icons; }
public List<Opt> getOpts() { return opts; }
}
public static class Opt {
private String name;
private String value;
public String getName() { return name; }
public String getValue() { return value; }
}
public String getTitle() { return title; }
public String getIcon() { return icon; }
public List<Pref> getPrefs() { return prefs; }
}
我用这个 -
反序列化json字符串objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
FeatureConfigData config = objectMapper.readValue(json, FeatureConfigData.class);
为什么配置字段在我的情况下是空的并且没有填充?我是否需要做其他事情以确保杰克逊知道地图键是“vertical1”,“vertical2”等?
注意,此配置驱动垂直的实际名称/编号。因此,不能替换地图&gt;使用包含字段vertical1,vertical2等的类。