Java Jackson:将带有动态名称的 JSON 反序列化为 JavaClass
Java Jackson: Deserialize JSON with dynamic Names into JavaClass
我正在尝试使用 jackson 反序列化 JSON。问题是字段名称总是在变化。
这是一个例子JSON:
{
2021-08-02: [
{
label: "OnlineGallery",
nb_uniq_visitors: 1,
nb_visits: 2,
nb_events: 2,
nb_events_with_value: 0,
sum_event_value: 0,
min_event_value: 0,
max_event_value: 0,
avg_event_value: 0,
segment: "eventCategory==OnlineGallery",
idsubdatatable: 1
}
],
2021-08-03: [ ],
2021-08-04: [ ]
}
我正在通过 Resttemplate 获取数据并尝试反序列化为 Java Class 但它不起作用:
private EventsGetCategory getEventFromAPI(Date startdate, Date enddate) {
RestTemplate restTemplate = new RestTemplate();
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
ResponseEntity<EventsGetCategory> response =
restTemplate.getForEntity(
matomoUrl + "index.php?module=API&label=OnlineGallery&method=Events.getCategory&secondaryDimension=eventAction&idSite=1&period=day&date=" + dateFormat.format(startdate) + "," + dateFormat.format(enddate) + "&format=JSON&filter_limit=-1"
+ "&token_auth=" + tokenAuth,
EventsGetCategory.class);
return response.getBody();
}
EventsGetCategory Class:
@Data
public class EventsGetCategory {
@JsonAnySetter
private Map<String, List<EventDetails>> details;
}
活动详情Class:
@Data
public class EventDetails {
String label;
int nb_uniq_visitors;
int nb_visits;
int nb_events;
int nb_events_with_value;
int sum_event_value;
int min_event_value;
int max_event_value;
int avg_event_value;
String segment;
int idsubdatatable;
}
有人可以帮我吗?
如果字段名称总是更改,那么您不能使用像 EventDetails 这样的 class 来将 JSON 对象映射到 java class,而不是 EventDetails class,您必须使用可以存储动态键值的 Map。
像这样更新您的 EventsGetCategory
class
public class EventsGetCategory {
private Map<String, List<EventDetails>> details;
@JsonAnySetter
public void setDetails(String key, List<EventDetails> val) {
if (this.details == null) {
this.details = new HashMap<>();
}
this.details.put(key, val);
}
}
我正在尝试使用 jackson 反序列化 JSON。问题是字段名称总是在变化。
这是一个例子JSON:
{
2021-08-02: [
{
label: "OnlineGallery",
nb_uniq_visitors: 1,
nb_visits: 2,
nb_events: 2,
nb_events_with_value: 0,
sum_event_value: 0,
min_event_value: 0,
max_event_value: 0,
avg_event_value: 0,
segment: "eventCategory==OnlineGallery",
idsubdatatable: 1
}
],
2021-08-03: [ ],
2021-08-04: [ ]
}
我正在通过 Resttemplate 获取数据并尝试反序列化为 Java Class 但它不起作用:
private EventsGetCategory getEventFromAPI(Date startdate, Date enddate) {
RestTemplate restTemplate = new RestTemplate();
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
ResponseEntity<EventsGetCategory> response =
restTemplate.getForEntity(
matomoUrl + "index.php?module=API&label=OnlineGallery&method=Events.getCategory&secondaryDimension=eventAction&idSite=1&period=day&date=" + dateFormat.format(startdate) + "," + dateFormat.format(enddate) + "&format=JSON&filter_limit=-1"
+ "&token_auth=" + tokenAuth,
EventsGetCategory.class);
return response.getBody();
}
EventsGetCategory Class:
@Data
public class EventsGetCategory {
@JsonAnySetter
private Map<String, List<EventDetails>> details;
}
活动详情Class:
@Data
public class EventDetails {
String label;
int nb_uniq_visitors;
int nb_visits;
int nb_events;
int nb_events_with_value;
int sum_event_value;
int min_event_value;
int max_event_value;
int avg_event_value;
String segment;
int idsubdatatable;
}
有人可以帮我吗?
如果字段名称总是更改,那么您不能使用像 EventDetails 这样的 class 来将 JSON 对象映射到 java class,而不是 EventDetails class,您必须使用可以存储动态键值的 Map
像这样更新您的 EventsGetCategory
class
public class EventsGetCategory {
private Map<String, List<EventDetails>> details;
@JsonAnySetter
public void setDetails(String key, List<EventDetails> val) {
if (this.details == null) {
this.details = new HashMap<>();
}
this.details.put(key, val);
}
}