gpt4 book ai didi

java - 如何在 Spring Boot 中使用 Java 从 JSON 响应中提取特定部分?

转载 作者:太空宇宙 更新时间:2023-11-04 09:22:05 24 4
gpt4 key购买 nike

我正在尝试从第 3 方 API 获取航类的当前状态。我只需要 JSON 中的很少几个字段。如果我以传统方式进行,我将不得不创建许多类。并且,将 json 映射到类。有更好的方法吗?在 JSON 响应中,我只需要 FlightStatuses 字段中的信息。

{
"request": {
"airline": {
"fsCode": "AA",
"requestedCode": "AA"
},
"flight": {
"requested": "100",
"interpreted": "100"
},
"utc": {
"requested": "false",
"interpreted": false
},
"url": "https://api.flightstats.com/flex/flightstatus/rest/v2/json/flight/status/AA/100/dep/2019/10/1?utc=false",
"nonstopOnly": {
"interpreted": false
},
"date": {
"year": "2019",
"month": "10",
"day": "1",
"interpreted": "2019-10-01"
}
},
"appendix": {
"airlines": [
{
"fs": "AA",
"iata": "AA",
"icao": "AAL",
"name": "American Airlines",
"phoneNumber": "08457-567-567",
"active": true
},
{
"fs": "AY",
"iata": "AY",
"icao": "FIN",
"name": "Finnair",
"phoneNumber": "+ 358 600 140 140",
"active": true
},
{
"fs": "IB",
"iata": "IB",
"icao": "IBE",
"name": "Iberia",
"phoneNumber": "1800 772 4642",
"active": true
},
{
"fs": "LY",
"iata": "LY",
"icao": "ELY",
"name": "El Al",
"phoneNumber": "+ 972-3-9771111",
"active": true
},
{
"fs": "BA",
"iata": "BA",
"icao": "BAW",
"name": "British Airways",
"phoneNumber": "1-800-AIRWAYS",
"active": true
},
{
"fs": "GF",
"iata": "GF",
"icao": "GFA",
"name": "Gulf Air",
"phoneNumber": "973 17 335 777",
"active": true
}
],
"airports": [
{
"fs": "LHR",
"iata": "LHR",
"icao": "EGLL",
"faa": "",
"name": "London Heathrow Airport",
"city": "London",
"cityCode": "LON",
"stateCode": "EN",
"countryCode": "GB",
"countryName": "United Kingdom",
"regionName": "Europe",
"timeZoneRegionName": "Europe/London",
"weatherZone": "",
"localTime": "2019-10-01T15:25:25.492",
"utcOffsetHours": 1.0,
"latitude": 51.469603,
"longitude": -0.453566,
"elevationFeet": 80,
"classification": 1,
"active": true,
"weatherUrl": "https://api.flightstats.com/flex/weather/rest/v1/json/all/LHR?codeType=fs",
"delayIndexUrl": "https://api.flightstats.com/flex/delayindex/rest/v1/json/airports/LHR?codeType=fs"
},
{
"fs": "JFK",
"iata": "JFK",
"icao": "KJFK",
"faa": "JFK",
"name": "John F. Kennedy International Airport",
"street1": "JFK Airport",
"city": "New York",
"cityCode": "NYC",
"stateCode": "NY",
"postalCode": "11430",
"countryCode": "US",
"countryName": "United States",
"regionName": "North America",
"timeZoneRegionName": "America/New_York",
"weatherZone": "NYZ178",
"localTime": "2019-10-01T10:25:25.493",
"utcOffsetHours": -4.0,
"latitude": 40.642335,
"longitude": -73.78817,
"elevationFeet": 13,
"classification": 1,
"active": true,
"weatherUrl": "https://api.flightstats.com/flex/weather/rest/v1/json/all/JFK?codeType=fs",
"delayIndexUrl": "https://api.flightstats.com/flex/delayindex/rest/v1/json/airports/JFK?codeType=fs"
}
],
"equipments": [
{
"iata": "77W",
"name": "Boeing 777-300ER",
"turboProp": false,
"jet": true,
"widebody": true,
"regional": false
}
]
},
"flightStatuses": [
{
"flightId": 1016157813,
"carrierFsCode": "AA",
"flightNumber": "100",
"departureAirportFsCode": "JFK",
"arrivalAirportFsCode": "LHR",
"departureDate": {
"dateUtc": "2019-10-01T22:15:00.000Z",
"dateLocal": "2019-10-01T18:15:00.000"
},
"arrivalDate": {
"dateUtc": "2019-10-02T05:20:00.000Z",
"dateLocal": "2019-10-02T06:20:00.000"
},
"status": "S",
"schedule": {
"flightType": "J",
"serviceClasses": "RFJY",
"restrictions": "",
"uplines": [],
"downlines": []
},
"operationalTimes": {
"publishedDeparture": {
"dateUtc": "2019-10-01T22:15:00.000Z",
"dateLocal": "2019-10-01T18:15:00.000"
},
"scheduledGateDeparture": {
"dateUtc": "2019-10-01T22:15:00.000Z",
"dateLocal": "2019-10-01T18:15:00.000"
},
"estimatedGateDeparture": {
"dateUtc": "2019-10-01T22:15:00.000Z",
"dateLocal": "2019-10-01T18:15:00.000"
},
"publishedArrival": {
"dateUtc": "2019-10-02T05:20:00.000Z",
"dateLocal": "2019-10-02T06:20:00.000"
},
"scheduledGateArrival": {
"dateUtc": "2019-10-02T05:20:00.000Z",
"dateLocal": "2019-10-02T06:20:00.000"
},
"estimatedGateArrival": {
"dateUtc": "2019-10-02T05:20:00.000Z",
"dateLocal": "2019-10-02T06:20:00.000"
}
},
"codeshares": [
{
"fsCode": "AY",
"flightNumber": "4012",
"relationship": "L"
},
{
"fsCode": "BA",
"flightNumber": "1511",
"relationship": "L"
},
{
"fsCode": "GF",
"flightNumber": "6654",
"relationship": "L"
},
{
"fsCode": "IB",
"flightNumber": "4218",
"relationship": "L"
},
{
"fsCode": "LY",
"flightNumber": "8051",
"relationship": "L"
}
],
"delays": {},
"flightDurations": {
"scheduledBlockMinutes": 425
},
"airportResources": {
"departureTerminal": "8",
"departureGate": "16",
"arrivalTerminal": "3"
},
"flightEquipment": {
"scheduledEquipmentIataCode": "77W",
"actualEquipmentIataCode": "77W",
"tailNumber": "N730AN"
}
}
]
}

最佳答案

您可以将 JSON 解析为 JsonNode。

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree("[your JSON here]");

之后仅获取您需要的字段。例如:

jsonNode.get("request").get("airline").get("requestedCode").asText()

关于java - 如何在 Spring Boot 中使用 Java 从 JSON 响应中提取特定部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58194101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com