在java中有段json字符串"{ "code":"000", "message":"成功", "data":[ { "flightId":1831011951, "flightDate":"2023-05-3000:00:00", "flightNo":"5101", "departCode":"SHA", "arriveCode":"PEK", "carrier":"MU", "flightStatus":"CALLING", "planDepartTime":"2023-05-3008:00:00", "planArriveTime":"2023-05-3010:15:00", "createTime":"2023-05-2413:35:58", "flightType":"CRE", "source":"MANUAL" } ], "success":true }"现在只要获取其中data里面的值,并且添加到 List<NotifyInfoCCResp>集合中,将完整代码展示出来
时间: 2023-08-21 20:04:00 浏览: 433
假设你已经定义好了 NotifyInfoCCResp 类,可以使用 Gson 库将 JSON 字符串转换成 Java 对象,然后取出 data 中的值,添加到 List<NotifyInfoCCResp> 集合中。代码如下:
```java
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
String json = "{ \"code\":\"000\", \"message\":\"成功\", \"data\":[ { \"flightId\":1831011951, \"flightDate\":\"2023-05-3000:00:00\", \"flightNo\":\"5101\", \"departCode\":\"SHA\", \"arriveCode\":\"PEK\", \"carrier\":\"MU\", \"flightStatus\":\"CALLING\", \"planDepartTime\":\"2023-05-3008:00:00\", \"planArriveTime\":\"2023-05-3010:15:00\", \"createTime\":\"2023-05-2413:35:58\", \"flightType\":\"CRE\", \"source\":\"MANUAL\" } ], \"success\":true }";
Gson gson = new Gson();
Type type = new TypeToken<Response>() {}.getType();
Response response = gson.fromJson(json, type);
List<NotifyInfoCCResp> notifyInfoList = new ArrayList<>();
for (NotifyInfo notifyInfo : response.getData()) {
NotifyInfoCCResp notifyInfoCCResp = new NotifyInfoCCResp();
notifyInfoCCResp.setFlightId(notifyInfo.getFlightId());
notifyInfoCCResp.setFlightDate(notifyInfo.getFlightDate());
notifyInfoCCResp.setFlightNo(notifyInfo.getFlightNo());
notifyInfoCCResp.setDepartCode(notifyInfo.getDepartCode());
notifyInfoCCResp.setArriveCode(notifyInfo.getArriveCode());
notifyInfoCCResp.setCarrier(notifyInfo.getCarrier());
notifyInfoCCResp.setFlightStatus(notifyInfo.getFlightStatus());
notifyInfoCCResp.setPlanDepartTime(notifyInfo.getPlanDepartTime());
notifyInfoCCResp.setPlanArriveTime(notifyInfo.getPlanArriveTime());
notifyInfoCCResp.setCreateTime(notifyInfo.getCreateTime());
notifyInfoCCResp.setFlightType(notifyInfo.getFlightType());
notifyInfoCCResp.setSource(notifyInfo.getSource());
notifyInfoList.add(notifyInfoCCResp);
}
System.out.println(notifyInfoList);
}
private static class Response {
private String code;
private String message;
private List<NotifyInfo> data;
private boolean success;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<NotifyInfo> getData() {
return data;
}
public void setData(List<NotifyInfo> data) {
this.data = data;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
}
private static class NotifyInfo {
private long flightId;
private String flightDate;
private String flightNo;
private String departCode;
private String arriveCode;
private String carrier;
private String flightStatus;
private String planDepartTime;
private String planArriveTime;
private String createTime;
private String flightType;
private String source;
public long getFlightId() {
return flightId;
}
public void setFlightId(long flightId) {
this.flightId = flightId;
}
public String getFlightDate() {
return flightDate;
}
public void setFlightDate(String flightDate) {
this.flightDate = flightDate;
}
public String getFlightNo() {
return flightNo;
}
public void setFlightNo(String flightNo) {
this.flightNo = flightNo;
}
public String getDepartCode() {
return departCode;
}
public void setDepartCode(String departCode) {
this.departCode = departCode;
}
public String getArriveCode() {
return arriveCode;
}
public void setArriveCode(String arriveCode) {
this.arriveCode = arriveCode;
}
public String getCarrier() {
return carrier;
}
public void setCarrier(String carrier) {
this.carrier = carrier;
}
public String getFlightStatus() {
return flightStatus;
}
public void setFlightStatus(String flightStatus) {
this.flightStatus = flightStatus;
}
public String getPlanDepartTime() {
return planDepartTime;
}
public void setPlanDepartTime(String planDepartTime) {
this.planDepartTime = planDepartTime;
}
public String getPlanArriveTime() {
return planArriveTime;
}
public void setPlanArriveTime(String planArriveTime) {
this.planArriveTime = planArriveTime;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getFlightType() {
return flightType;
}
public void setFlightType(String flightType) {
this.flightType = flightType;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
}
private static class NotifyInfoCCResp {
private long flightId;
private String flightDate;
private String flightNo;
private String departCode;
private String arriveCode;
private String carrier;
private String flightStatus;
private String planDepartTime;
private String planArriveTime;
private String createTime;
private String flightType;
private String source;
public long getFlightId() {
return flightId;
}
public void setFlightId(long flightId) {
this.flightId = flightId;
}
public String getFlightDate() {
return flightDate;
}
public void setFlightDate(String flightDate) {
this.flightDate = flightDate;
}
public String getFlightNo() {
return flightNo;
}
public void setFlightNo(String flightNo) {
this.flightNo = flightNo;
}
public String getDepartCode() {
return departCode;
}
public void setDepartCode(String departCode) {
this.departCode = departCode;
}
public String getArriveCode() {
return arriveCode;
}
public void setArriveCode(String arriveCode) {
this.arriveCode = arriveCode;
}
public String getCarrier() {
return carrier;
}
public void setCarrier(String carrier) {
this.carrier = carrier;
}
public String getFlightStatus() {
return flightStatus;
}
public void setFlightStatus(String flightStatus) {
this.flightStatus = flightStatus;
}
public String getPlanDepartTime() {
return planDepartTime;
}
public void setPlanDepartTime(String planDepartTime) {
this.planDepartTime = planDepartTime;
}
public String getPlanArriveTime() {
return planArriveTime;
}
public void setPlanArriveTime(String planArriveTime) {
this.planArriveTime = planArriveTime;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getFlightType() {
return flightType;
}
public void setFlightType(String flightType) {
this.flightType = flightType;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
@Override
public String toString() {
return "NotifyInfoCCResp{" +
"flightId=" + flightId +
", flightDate='" + flightDate + '\'' +
", flightNo='" + flightNo + '\'' +
", departCode='" + departCode + '\'' +
", arriveCode='" + arriveCode + '\'' +
", carrier='" + carrier + '\'' +
", flightStatus='" + flightStatus + '\'' +
", planDepartTime='" + planDepartTime + '\'' +
", planArriveTime='" + planArriveTime + '\'' +
", createTime='" + createTime + '\'' +
", flightType='" + flightType + '\'' +
", source='" + source + '\'' +
'}';
}
}
}
```
输出结果如下:
```
[NotifyInfoCCResp{flightId=1831011951, flightDate='2023-05-3000:00:00', flightNo='5101', departCode='SHA', arriveCode='PEK', carrier='MU', flightStatus='CALLING', planDepartTime='2023-05-3008:00:00', planArriveTime='2023-05-3010:15:00', createTime='2023-05-2413:35:58', flightType='CRE', source='MANUAL'}]
```
阅读全文