繁体   English   中英

如何获取列表格式的 JSON 响应

[英]How to get JSON Response of List Format

我收到了这种格式的 Jason 回复。

[
    {
        "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"
    }
]

任何人都可以帮助我如何使用 Java 代码在我的 Serenity 中获取这种格式。

如果你分享你到目前为止尝试过的东西,那将是最好的,但无论如何都在这里。

假设您正在使用某种对象映射器(即 Jackson 可能是一个潜在的候选者),您首先需要一个 pojo 类来映射您的请求正文。

在你的情况下,这看起来像这样:

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 + '\'' +
            '}';
    }

}

有了这个,阅读对象列表就很容易了,归结为:

 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();
    }
 }

希望这可以帮助!

暂无
暂无

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

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