简体   繁体   English

如何使用杰克逊获取json响应?

[英]How to get a json response using jackson?

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. 我是jackson的新手,我有一个示例json输出,需要使用jackson来获取此数据作为响应,我为此创建了pojo类,因为我知道可以通过json简单实现这种问题,但是我是不知道要用杰克逊做这个。 Please can some one provide some help. 请能有人提供一些帮助。

This is my sample json Data - 这是我的示例json数据-

{
"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-- 这些是我的pojo课程-

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. 提前致谢。

You should create another class (rootPojo) with an attribute array called apmmetrics of MetricsCollection . 您应该使用称为apmmetrics of MetricsCollection的属性数组创建另一个类(rootPojo)。 Then generate the json: mapper.writeValueAsString(rootPojo); 然后生成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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM