Reputation: 1793
I am new to jackson, and I had a sample json output need to use jackson to get this data as response, I had created the pojo classes for that, as I know this kind of problem to be achieved by json simple, but I am not getting any idea to do this usong jackson. Please can some one provide some help.
This is my sample json Data -
{
"apmmetrics": [
{
"metric": "Somevalue",
"level": "somevalue",
"data": [
{
"host": "someValue",
"instance": "someValue",
"app": "someValue",
"series": [
{
"bucket": "201607210949",
"max": 300,
"min": 15,
"avg": 57.55,
"total": 1899,
"count": 33
},
{
"bucket": "201607210948",
"max": 437,
"min": 13,
"avg": 93.5,
"total": 13464,
"count": 144
},
{
"bucket": "201607210947",
"max": 431,
"min": 13,
"avg": 86.25,
"total": 28376,
"count": 329
}
]
}
]
}
]}
These are my pojo classes--
public class MetricsCollection {
private String metric;
private String level;
private List<MetricsGroup> data;
private transient Map<String, MetricsGroup> meta;
}
public class MetricsGroup {
private String host;
private String instance;
private String app;
private List<GenericMetrics> series;
private transient Map<String, GenericMetrics> metaMap;
}
public class BaseMetrics implements Serializable {
private static final long serialVersionUID = -3249688349785265214L;
protected double max = 0.0;
protected double min = 0.0;
protected double avg = 0.0;
protected double total = 0.0;
protected long count = 0;
}
public class GenericMetrics extends BaseMetrics {
private static final long serialVersionUID = -9057601499394607167L;
private String bucket;
private transient long rc = 0;
}
please let me know how can I achieve this. It will be really thankful. Thanks in advance.
Upvotes: 0
Views: 944
Reputation: 830
You should create another class (rootPojo) with an attribute array called apmmetrics
of MetricsCollection
.
Then generate the json: mapper.writeValueAsString(rootPojo);
EDIT:
Or just:
final ObjectMapper mapper = new ObjectMapper();
final Map<String, List<MetricsCollection>> dataMap = ...
dataMap.put("apmmetrics", listOfMetricsCollection);
System.out.println(mapper.writeValueAsString(dataMap));
Upvotes: 1