简体   繁体   中英

How to get JSON Response of List Format

I am getting a Jason response of this format.

[
    {
        "total": "5",
        "tracking_id": 780,
        "type": "Unplanned",
        "severity": "High",
        "NOTIFICATION_TYPE": "FINAL",
        "Service": "M2MMC-Notification",
        "location": "Billerica",
        "state": "MA",
        "lat_dec": "42.5663890000",
        "long_dec": "-71.3236110000",
        "Maintenance_Performed": "",
        "Work_Performed": ""
    },
    {
        "total": "5",
        "tracking_id": 581,
        "type": "Unplanned",
        "severity": "High",
        "NOTIFICATION_TYPE": "FINAL",
        "Service": "M2MMC-Notification",
        "location": "Billerica",
        "state": "MA",
        "lat_dec": "42.5663890000",
        "long_dec": "-71.3236110000",
        "Maintenance_Performed": "",
        "Work_Performed": ""
    },
    {
        "total": "5",
        "tracking_id": 598,
        "type": "Planned",
        "severity": "LOW",
        "NOTIFICATION_TYPE": "SUBMITTED",
        "Service": "M2MMC-Notification",
        "location": "Billerica",
        "state": "MA",
        "lat_dec": "42.5663890000",
        "long_dec": "-71.3236110000",
        "Maintenance_Performed": "BREAKFIXAPPLICATION",
        "Work_Performed": "VOLTE"
    },
    {
        "total": "5",
        "tracking_id": 791,
        "type": "Unplanned",
        "severity": "High",
        "NOTIFICATION_TYPE": "FINAL",
        "Service": "M2MMC-Notification",
        "location": "Billerica",
        "state": "MA",
        "lat_dec": "42.5663890000",
        "long_dec": "-71.3236110000",
        "Maintenance_Performed": "",
        "Work_Performed": ""
    },
    {
        "total": "5",
        "tracking_id": 795,
        "type": "Planned",
        "severity": "LOW",
        "NOTIFICATION_TYPE": "CANCELLED",
        "Service": "M2MMC-Notification",
        "location": "Nationwide",
        "state": "",
        "lat_dec": "0",
        "long_dec": "0",
        "Maintenance_Performed": "BREAKFIXAPPLICATION",
        "Work_Performed": "VOLTE"
    }
]

Can any one please help me in how to get this format in my Serenity with Java code.

It would be best if you shared what you tried so far, but anyways here goes.

Assuming you're using some kind of object mapper (ie Jackson could be a potential candidate), you'll first need a pojo class mapping the body of your request.

In your case this would look like this:

import com.fasterxml.jackson.annotation.JsonProperty;

public class Pojo {

    private String total;
    public String getTotal() { return total; }
    public void setTotal(String total) { this.total = total; }

    @JsonProperty("tracking_id")
    private Integer trackingId;
    public Integer getTrackingId() { return trackingId; }
    public void setTrackingId(Integer trackingId) { this.trackingId = trackingId; }

    private String type;
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }

    private String severity;
    public String getSeverity() { return severity; }
    public void setSeverity(String severity) { this.severity = severity; }

    @JsonProperty("NOTIFICATION_TYPE")
    private String notificationType;
    public String getNotificationType() { return notificationType; }
    public void setNotificationType(String notificationType) { this.notificationType = notificationType; }

    @JsonProperty("Service")
    private String service;
    public String getService() { return service; }
    public void setService(String service) { this.service = service; }

    private String location;
    public String getLocation() { return location; }
    public void setLocation(String location) { this.location = location; }

    private String state;
    public String getState() { return state; }
    public void setState(String state) { this.state = state; }

    @JsonProperty("lat_dec")
    private Double latDec;
    public Double getLatDec() { return latDec; }
    public void setLatDec(Double latDec) { this.latDec = latDec; }

    @JsonProperty("long_dec")
    private Double longDec;
    public Double getLongDec() { return longDec; }
    public void setLongDec(Double longDec) { this.longDec = longDec; }

    @JsonProperty("Maintenance_Performed")
    private String maintenancePerformed;
    public String getMaintancePerformed() { return maintenancePerformed; }
    public void setMaintancePerformed(String maintancePerformed) { this.maintenancePerformed = maintancePerformed; }

    @JsonProperty("Work_Performed")
    private String workPerformed;
    public String getMaintenancePerformed() { return maintenancePerformed; }
    public void setMaintenancePerformed(String maintenancePerformed) { this.maintenancePerformed = maintenancePerformed; }

    @Override
    public String toString() {
        return "Pojo{" +
            "total='" + total + '\'' +
            ", trackingId=" + trackingId +
            ", type='" + type + '\'' +
            ", severity='" + severity + '\'' +
            ", notificationType='" + notificationType + '\'' +
            ", service='" + service + '\'' +
            ", location='" + location + '\'' +
            ", state='" + state + '\'' +
            ", latDec=" + latDec +
            ", longDec=" + longDec +
            ", maintenancePerformed='" + maintenancePerformed + '\'' +
            ", workPerformed='" + workPerformed + '\'' +
            '}';
    }

}

With this in hand reading the list of objects is quite easy and boils down to this:

 public static void main(String... args) {
    try {
        List<Pojo> pojoList = new ObjectMapper().readValue(JSON_BODY, new TypeReference<>() {});
        pojoList.forEach(System.out::println);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
 }

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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