Java 如何获取列表格式的JSON响应
我得到了这种格式的Jason回复Java 如何获取列表格式的JSON响应,java,serenity-bdd,Java,Serenity Bdd,我得到了这种格式的Jason回复 [ { "total": "5", "tracking_id": 780, "type": "Unplanned", "severity": "High", "NOTIFICATION_TYPE": "FINAL", "Service": "M2MMC-Notification", "location": "Billerica",
[
{
"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();
}
}
publicstaticvoidmain(字符串…参数){
试一试{
List pojoList=new ObjectMapper().readValue(JSON_BODY,new TypeReference(){});
pojoList.forEach(System.out::println);
}捕获(JsonProcessingException e){
e、 printStackTrace();
}
}
希望这有帮助 如果你能分享你迄今为止所做的尝试,那就最好了,但不管怎样,还是这样吧 假设您正在使用某种对象映射器(即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();
}
}
publicstaticvoidmain(字符串…参数){
试一试{
List pojoList=new ObjectMapper().readValue(JSON_BODY,new TypeReference(){});
pojoList.forEach(System.out::println);
}捕获(JsonProcessingException e){
e、 printStackTrace();
}
}
希望这有帮助 您好,我看到您是新来的堆栈溢出-欢迎来到社区。这是一个好主意,尝试和搜索您的答案在线(谷歌等),如果你没有找到任何答案,请描述你已经寻找。嗨,我看到你是新的堆栈溢出-欢迎来到社区。这是一个好主意,尝试和搜索您的答案在线(谷歌等),如果你没有找到任何答案,请描述你已经寻找。