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

标签 java spring spring-boot

我正在尝试从第 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/

相关文章:

java - Spring v3 找不到元素 'mvc:resources' 的声明

spring - 配置类中@EnableWebSecurity 的原因

java - 如何使用 FTPSClient 和 PuTTY 格式的 key

java - 我可以使用 C++ Boost shared_ptr 进行编程,就好像我在用 Java 编写代码一样,而不关心内存管理吗?

java - 没有celery如何回复celery任务?

java - 注释顺序在 SB 应用程序中是否重要?

spring - Spring命令行JSON配置包含数组

java - Android:如何捕获此异常

javascript - 在 jBoss 6.2 和 jBoss 7.3 中部署应用程序时的不同响应

spring-boot - 如何为Apache Velocity禁用Spring Boot的自动配置?