在 Jackson 中映射具有变量名称的对象
Mapping an Object with variable name in Jackson
我有以下 JSON:
{
"filebeat": {
"version": 2,
"modified_date": "2021-01-15T14:02:41.103Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "30gb",
"max_age": "1d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"wait_for_snapshot": {
"policy": "mainbackuppolicy"
}
}
}
}
}
},
"ilm-history-ilm-policy": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.717Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {
"delete_searchable_snapshot": true
}
}
}
}
}
},
"kibana-event-log-policy": {
"version": 3,
"modified_date": "2020-12-08T14:39:00.097Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {
"delete_searchable_snapshot": true
}
}
}
}
}
},
"logs": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.227Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
}
}
}
},
"metricbeat": {
"version": 2,
"modified_date": "2021-01-15T14:02:42.090Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "30gb",
"max_age": "1d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"wait_for_snapshot": {
"policy": "mainbackuppolicy"
}
}
}
}
}
},
"metrics": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.475Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
}
}
}
},
"ml-size-based-ilm-policy": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.083Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb"
}
}
}
}
}
},
"slm-history-ilm-policy": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.585Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {
"delete_searchable_snapshot": true
}
}
}
}
}
},
"synthetics": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.352Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
}
}
}
},
"watch-history-ilm-policy": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.792Z",
"policy": {
"phases": {
"delete": {
"min_age": "7d",
"actions": {
"delete": {
"delete_searchable_snapshot": true
}
}
}
}
}
}
}
我不知道如何将这些 JSON 元素映射到相应的对象,因为它的名称是可变的,例如filebeat、ilm-history-ilm-policy 等
我创建了以下对象:
public class IndexLifeCycleManagement {
private IndexLifeCycleManagementPolicy indexLifeCycleManagementPolicy;
}
public class IndexLifeCycleManagementPolicy {
@JsonProperty("modified_date")
private String modifiedDate;
@JsonProperty("version")
private int version;
@JsonProperty("policy")
private Policy policy;
}
但我认为问题在于 Jackson 无法将 filebeat 映射到 IndexLifecycleManagement-object
提前致谢
此致
可以,您需要创建一个对象,该对象将包含 IndexLifeCycleManagementPolicy 类型的名为 filebeat、ilm-history-ilm-policy 等的字段。
示例:
Class YourJsonObject
私有 IndexLifeCycleManagementPolicy ilm-history-ilm-policy;
private IndexLifeCycleManagementPolicy fileBeat
等等
将 IndexLifeCycleManagementPolicy
的每个实例与其对应的 JSON 属性映射,如下所示:
class IndexLifeCycleManagementPolicy{
@JsonProperty("modified_date")
private String modifiedDate;
@JsonProperty("version")
private int version;
@JsonProperty("policy")
private Policy policy;
}
class IndexLifeCycleManagement{
@JsonProperty("ilm-history-ilm-policy")
public IndexLifeCycleManagementPolicy ilmHistoryIlmPolicy;
@JsonProperty("filebeat")
public IndexLifeCycleManagementPolicy filebeat;
// similarly for every other attribute at this level
}
编辑 1:
您可以使用 IndexLifeCycleManagementPolicy
的 Map 并通过 JsonAnySetter
注释动态设置它们,如下所示:
class IndexLifeCycleManagement{
public Map<String, IndexLifeCycleManagementPolicy> getIndexLifeCycleManagementPolicyMap() {
return indexLifeCycleManagementPolicyMap;
}
@JsonAnySetter
public void setIndexLifeCycleManagementPolicyMap(String key, IndexLifeCycleManagementPolicy policy) {
indexLifeCycleManagementPolicyMap.put(key, policy);
}
Map<String, IndexLifeCycleManagementPolicy> indexLifeCycleManagementPolicyMap = new HashMap<>();
}
例如: key = "filebeat" and value = IndexLifeCycleManagementPolicy
instance.
然后你可以有一个 getter 来阅读这个包含 IndexLifeCycleManagementPolicy
个实例的地图。
我有以下 JSON:
{
"filebeat": {
"version": 2,
"modified_date": "2021-01-15T14:02:41.103Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "30gb",
"max_age": "1d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"wait_for_snapshot": {
"policy": "mainbackuppolicy"
}
}
}
}
}
},
"ilm-history-ilm-policy": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.717Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {
"delete_searchable_snapshot": true
}
}
}
}
}
},
"kibana-event-log-policy": {
"version": 3,
"modified_date": "2020-12-08T14:39:00.097Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {
"delete_searchable_snapshot": true
}
}
}
}
}
},
"logs": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.227Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
}
}
}
},
"metricbeat": {
"version": 2,
"modified_date": "2021-01-15T14:02:42.090Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "30gb",
"max_age": "1d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"wait_for_snapshot": {
"policy": "mainbackuppolicy"
}
}
}
}
}
},
"metrics": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.475Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
}
}
}
},
"ml-size-based-ilm-policy": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.083Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb"
}
}
}
}
}
},
"slm-history-ilm-policy": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.585Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {
"delete_searchable_snapshot": true
}
}
}
}
}
},
"synthetics": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.352Z",
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
}
}
}
},
"watch-history-ilm-policy": {
"version": 1,
"modified_date": "2020-12-08T14:31:15.792Z",
"policy": {
"phases": {
"delete": {
"min_age": "7d",
"actions": {
"delete": {
"delete_searchable_snapshot": true
}
}
}
}
}
}
}
我不知道如何将这些 JSON 元素映射到相应的对象,因为它的名称是可变的,例如filebeat、ilm-history-ilm-policy 等
我创建了以下对象:
public class IndexLifeCycleManagement {
private IndexLifeCycleManagementPolicy indexLifeCycleManagementPolicy;
}
public class IndexLifeCycleManagementPolicy {
@JsonProperty("modified_date")
private String modifiedDate;
@JsonProperty("version")
private int version;
@JsonProperty("policy")
private Policy policy;
}
但我认为问题在于 Jackson 无法将 filebeat 映射到 IndexLifecycleManagement-object
提前致谢 此致
可以,您需要创建一个对象,该对象将包含 IndexLifeCycleManagementPolicy 类型的名为 filebeat、ilm-history-ilm-policy 等的字段。 示例:
Class YourJsonObject 私有 IndexLifeCycleManagementPolicy ilm-history-ilm-policy; private IndexLifeCycleManagementPolicy fileBeat 等等
将 IndexLifeCycleManagementPolicy
的每个实例与其对应的 JSON 属性映射,如下所示:
class IndexLifeCycleManagementPolicy{
@JsonProperty("modified_date")
private String modifiedDate;
@JsonProperty("version")
private int version;
@JsonProperty("policy")
private Policy policy;
}
class IndexLifeCycleManagement{
@JsonProperty("ilm-history-ilm-policy")
public IndexLifeCycleManagementPolicy ilmHistoryIlmPolicy;
@JsonProperty("filebeat")
public IndexLifeCycleManagementPolicy filebeat;
// similarly for every other attribute at this level
}
编辑 1:
您可以使用 IndexLifeCycleManagementPolicy
的 Map 并通过 JsonAnySetter
注释动态设置它们,如下所示:
class IndexLifeCycleManagement{
public Map<String, IndexLifeCycleManagementPolicy> getIndexLifeCycleManagementPolicyMap() {
return indexLifeCycleManagementPolicyMap;
}
@JsonAnySetter
public void setIndexLifeCycleManagementPolicyMap(String key, IndexLifeCycleManagementPolicy policy) {
indexLifeCycleManagementPolicyMap.put(key, policy);
}
Map<String, IndexLifeCycleManagementPolicy> indexLifeCycleManagementPolicyMap = new HashMap<>();
}
例如: key = "filebeat" and value = IndexLifeCycleManagementPolicy
instance.
然后你可以有一个 getter 来阅读这个包含 IndexLifeCycleManagementPolicy
个实例的地图。