0

我从某个 URL 获得的以下结果。我需要从 php 中的以下 json 响应中获取一些值

{"request":{"airport":{"requestedCode":"IXJ","fsCode":"IXJ"},"date":{"year":"2013","month":"4","day":"18","interpreted":"2013-04-18"},"hourOfDay":{"requested":"12","interpreted":12},"numHours":{"requested":"6","interpreted":6},"utc":{"requested":"false","interpreted":false},"codeType":{},"maxFlights":{"requested":"1","interpreted":1},"extendedOptions":{},"url":"https://api.flightstats.com/flex/flightstatus/rest/v2/json/airport/status/IXJ/dep/2013/04/18/12"},"appendix":{"airlines":[{"fs":"G8","iata":"G8","icao":"GOW","name":"GoAir","active":true}],"airports":[{"fs":"BOM","iata":"BOM","icao":"VABB","name":"Chhatrapati Shivaji International Airport","city":"Mumbai","cityCode":"BOM","countryCode":"IN","countryName":"India","regionName":"Asia","timeZoneRegionName":"Asia/Kolkata","localTime":"2013-04-18T08:37:27.791","utcOffsetHours":5.5,"latitude":19.095508,"longitude":72.874969,"elevationFeet":27,"classification":1,"active":true,"delayIndexUrl":"https://api.flightstats.com/flex/delayindex/rest/v1/json/airports/BOM?codeType=fs","weatherUrl":"https://api.flightstats.com/flex/weather/rest/v1/json/all/BOM?codeType=fs"},{"fs":"SXR","iata":"SXR","icao":"VISR","name":"Srinagar Airport","city":"Srinagar","cityCode":"SXR","countryCode":"IN","countryName":"India","regionName":"Asia","timeZoneRegionName":"Asia/Kolkata","localTime":"2013-04-18T08:37:27.791","utcOffsetHours":5.5,"latitude":34.002308,"longitude":74.762631,"elevationFeet":5436,"classification":4,"active":true,"delayIndexUrl":"https://api.flightstats.com/flex/delayindex/rest/v1/json/airports/SXR?codeType=fs","weatherUrl":"https://api.flightstats.com/flex/weather/rest/v1/json/all/SXR?codeType=fs"},{"fs":"IXJ","iata":"IXJ","icao":"VIJU","name":"Satwari Airport","city":"Jammu","cityCode":"IXJ","countryCode":"IN","countryName":"India","regionName":"Asia","timeZoneRegionName":"Asia/Kolkata","localTime":"2013-04-18T08:37:27.387","utcOffsetHours":5.5,"latitude":32.68077,"longitude":74.842824,"elevationFeet":1029,"classification":4,"active":true,"delayIndexUrl":"https://api.flightstats.com/flex/delayindex/rest/v1/json/airports/IXJ?codeType=fs","weatherUrl":"https://api.flightstats.com/flex/weather/rest/v1/json/all/IXJ?codeType=fs"}],"equipments":[{"iata":"320","name":"Airbus Industrie A320","turboProp":false,"jet":true,"widebody":false,"regional":false}]},"flightStatuses":[{"flightId":294315800,"carrierFsCode":"G8","flightNumber":"387","departureAirportFsCode":"IXJ","arrivalAirportFsCode":"BOM","departureDate":{"dateLocal":"2013-04-18T15:15:00.000","dateUtc":"2013-04-18T09:45:00.000Z"},"arrivalDate":{"dateLocal":"2013-04-18T18:05:00.000","dateUtc":"2013-04-18T12:35:00.000Z"},"status":"S","schedule":{"flightType":"J","serviceClasses":"RFJY","restrictions":"","uplines":[{"fsCode":"BOM","flightId":294284559},{"fsCode":"SXR","flightId":294354303}]},"operationalTimes":{"publishedDeparture":{"dateLocal":"2013-04-18T15:15:00.000","dateUtc":"2013-04-18T09:45:00.000Z"},"publishedArrival":{"dateLocal":"2013-04-18T18:05:00.000","dateUtc":"2013-04-18T12:35:00.000Z"},"scheduledGateDeparture":{"dateLocal":"2013-04-18T15:15:00.000","dateUtc":"2013-04-18T09:45:00.000Z"},"scheduledGateArrival":{"dateLocal":"2013-04-18T18:05:00.000","dateUtc":"2013-04-18T12:35:00.000Z"}},"flightDurations":{"scheduledBlockMinutes":170},"airportResources":{"arrivalTerminal":"1B"},"flightEquipment":{"scheduledEquipmentIataCode":"320"}}]}

就像我只想获取 carrierFsCode,flightNumber,departureAirportFsCode,arrivalAirportFsCode,departureDate,arrivalDate,status,scheduledGateDeparture,flightDurations,flightDurations

json 响应中有许多航班状态。有人可以帮我获得这些值吗?

4

2 回答 2

2

你似乎已经用 json-decode 标记了这个。有什么特别的原因不适合你吗?

$json = ''; //your json string here
$obj = json_decode($json);

var_dump($obj);

编辑:更新答案以免讨论延长这么长时间。为了获取所有状态,您需要为 flightStatuses 数组运行一个 foreach 循环。

foreach ($obj->flightStatuses as $flightstatus) {
    echo $flightstatus->flightId,"<br>";
}
于 2013-04-18T02:30:09.360 回答
0

您可以使用json_decode将此 JSON 转换为关联数组。然后,您可以轻松选择所需的值。假设您的 JSON 包含在名为 $data 的变量中

$associative_array = json_decode($data, true);
$carrierFsCode = $associative_array['carrierFsCode'];
$flightNumber = $associative_array['flightNumber'];

... ETC

于 2013-04-18T02:37:29.103 回答