donglin1692 2017-02-23 13:47
浏览 43
已采纳

在PHP中解析QPX Express API响应

I am creating a travel application and trying to implement the QPX express API. I've had some success, but I am looking for some help.

I am trying to parse the arrival and departure time, which is located in trips/tripOption/slice/segment/leg.

I am trying to parse from the following code: (sorry for being so long)

{
    "kind": "qpxExpress#tripsSearch", "trips": {

        "kind": "qpxexpress#tripOptions",
        "requestId": "zGRd2g7SO2OR3uDEu0Q3Rc",
        "data": {
            "kind": "qpxexpress#data",
            "airport": [
              {
                  "kind": "qpxexpress#airportData",
                  "code": "ABZ",
                  "city": "ABZ",
                  "name": "Aberdeen Dyce"
              },
              {
                  "kind": "qpxexpress#airportData",
                  "code": "LHR",
                  "city": "LON",
                  "name": "London Heathrow"
              }
            ],
            "city": [
              {
                  "kind": "qpxexpress#cityData",
                  "code": "ABZ",
                  "name": "Aberdeen"
              },
              {
                  "kind": "qpxexpress#cityData",
                  "code": "LON",
                  "name": "London"
              }
            ],
            "aircraft": [
              {
                  "kind": "qpxexpress#aircraftData",
                  "code": "319",
                  "name": "Airbus A319"
              },
              {
                  "kind": "qpxexpress#aircraftData",
                  "code": "320",
                  "name": "Airbus A320"
              },
              {
                  "kind": "qpxexpress#aircraftData",
                  "code": "321",
                  "name": "Airbus A321"
              }
            ],
            "tax": [
              {
                  "kind": "qpxexpress#taxData",
                  "id": "UB_001",
                  "name": "United Kingdom Passenger Service Charge Departures"
              },
              {
                  "kind": "qpxexpress#taxData",
                  "id": "GB_001",
                  "name": "United Kingdom Air Passenger Duty Apd"
              }
            ],
            "carrier": [
              {
                  "kind": "qpxexpress#carrierData",
                  "code": "BA",
                  "name": "British Airways"
              }
            ]
        },
        "tripOption": [
          {
              "kind": "qpxexpress#tripOption",
              "saleTotal": "GBP185.93",
              "id": "GNz9mcDQwhANkULUdCtRhR002",
              "slice": [
                {
                    "kind": "qpxexpress#sliceInfo",
                    "duration": 100,
                    "segment": [
                      {
                          "kind": "qpxexpress#segmentInfo",
                          "duration": 100,
                          "flight": {
                              "carrier": "BA",
                              "number": "1309"
                          },
                          "id": "G-8Do8J4Ed1CCSbw",
                          "cabin": "COACH",
                          "bookingCode": "H",
                          "bookingCodeCount": 2,
                          "marriedSegmentGroup": "0",
                          "leg": [
                            {
                                "kind": "qpxexpress#legInfo",
                                "id": "LeISp98KIk6mBaM9",
                                "aircraft": "320",
                                "arrivalTime": "2017-02-25T16:15+00:00",
                                "departureTime": "2017-02-25T14:35+00:00",
                                "origin": "ABZ",
                                "destination": "LHR",
                                "destinationTerminal": "5",
                                "duration": 100,
                                "mileage": 401,
                                "meal": "Food and Beverages for Purchase"
                            }
                          ]
                      }
                    ]
                }
              ],
              "pricing": [
                {
                    "kind": "qpxexpress#pricingInfo",
                    "fare": [
                      {
                          "kind": "qpxexpress#fareInfo",
                          "id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "carrier": "BA",
                          "origin": "ABZ",
                          "destination": "LON",
                          "basisCode": "HV2RO"
                      }
                    ],
                    "segmentPricing": [
                      {
                          "kind": "qpxexpress#segmentPricing",
                          "fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "segmentId": "G-8Do8J4Ed1CCSbw"
                      }
                    ],
                    "baseFareTotal": "GBP161.00",
                    "saleFareTotal": "GBP161.00",
                    "saleTaxTotal": "GBP24.93",
                    "saleTotal": "GBP185.93",
                    "passengers": {
                        "kind": "qpxexpress#passengerCounts",
                        "adultCount": 1
                    },
                    "tax": [
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "UB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "UB",
                          "country": "GB",
                          "salePrice": "GBP11.93"
                      },
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "GB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "GB",
                          "country": "GB",
                          "salePrice": "GBP13.00"
                      }
                    ],
                    "fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
                    "latestTicketingTime": "2017-02-25T09:34-05:00",
                    "ptc": "ADT"
                }
              ]
          },
          {
              "kind": "qpxexpress#tripOption",
              "saleTotal": "GBP185.93",
              "id": "GNz9mcDQwhANkULUdCtRhR005",
              "slice": [
                {
                    "kind": "qpxexpress#sliceInfo",
                    "duration": 105,
                    "segment": [
                      {
                          "kind": "qpxexpress#segmentInfo",
                          "duration": 105,
                          "flight": {
                              "carrier": "BA",
                              "number": "1303"
                          },
                          "id": "G8zQDGLwjfHKE25K",
                          "cabin": "COACH",
                          "bookingCode": "H",
                          "bookingCodeCount": 2,
                          "marriedSegmentGroup": "0",
                          "leg": [
                            {
                                "kind": "qpxexpress#legInfo",
                                "id": "LWDGrQuSyJsv4smo",
                                "aircraft": "319",
                                "arrivalTime": "2017-02-25T09:30+00:00",
                                "departureTime": "2017-02-25T07:45+00:00",
                                "origin": "ABZ",
                                "destination": "LHR",
                                "destinationTerminal": "5",
                                "duration": 105,
                                "mileage": 401,
                                "meal": "Food and Beverages for Purchase"
                            }
                          ]
                      }
                    ]
                }
              ],
              "pricing": [
                {
                    "kind": "qpxexpress#pricingInfo",
                    "fare": [
                      {
                          "kind": "qpxexpress#fareInfo",
                          "id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "carrier": "BA",
                          "origin": "ABZ",
                          "destination": "LON",
                          "basisCode": "HV2RO"
                      }
                    ],
                    "segmentPricing": [
                      {
                          "kind": "qpxexpress#segmentPricing",
                          "fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "segmentId": "G8zQDGLwjfHKE25K"
                      }
                    ],
                    "baseFareTotal": "GBP161.00",
                    "saleFareTotal": "GBP161.00",
                    "saleTaxTotal": "GBP24.93",
                    "saleTotal": "GBP185.93",
                    "passengers": {
                        "kind": "qpxexpress#passengerCounts",
                        "adultCount": 1
                    },
                    "tax": [
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "UB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "UB",
                          "country": "GB",
                          "salePrice": "GBP11.93"
                      },
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "GB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "GB",
                          "country": "GB",
                          "salePrice": "GBP13.00"
                      }
                    ],
                    "fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
                    "latestTicketingTime": "2017-02-25T02:44-05:00",
                    "ptc": "ADT"
                }
              ]
          },
          {
              "kind": "qpxexpress#tripOption",
              "saleTotal": "GBP185.93",
              "id": "GNz9mcDQwhANkULUdCtRhR004",
              "slice": [
                {
                    "kind": "qpxexpress#sliceInfo",
                    "duration": 100,
                    "segment": [
                      {
                          "kind": "qpxexpress#segmentInfo",
                          "duration": 100,
                          "flight": {
                              "carrier": "BA",
                              "number": "1311"
                          },
                          "id": "GrrTYvxj3OBwLUle",
                          "cabin": "COACH",
                          "bookingCode": "H",
                          "bookingCodeCount": 1,
                          "marriedSegmentGroup": "0",
                          "leg": [
                            {
                                "kind": "qpxexpress#legInfo",
                                "id": "LLa2rlu3AEMaQJ-7",
                                "aircraft": "320",
                                "arrivalTime": "2017-02-25T14:00+00:00",
                                "departureTime": "2017-02-25T12:20+00:00",
                                "origin": "ABZ",
                                "destination": "LHR",
                                "destinationTerminal": "5",
                                "duration": 100,
                                "mileage": 401,
                                "meal": "Food and Beverages for Purchase"
                            }
                          ]
                      }
                    ]
                }
              ],
              "pricing": [
                {
                    "kind": "qpxexpress#pricingInfo",
                    "fare": [
                      {
                          "kind": "qpxexpress#fareInfo",
                          "id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "carrier": "BA",
                          "origin": "ABZ",
                          "destination": "LON",
                          "basisCode": "HV2RO"
                      }
                    ],
                    "segmentPricing": [
                      {
                          "kind": "qpxexpress#segmentPricing",
                          "fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "segmentId": "GrrTYvxj3OBwLUle"
                      }
                    ],
                    "baseFareTotal": "GBP161.00",
                    "saleFareTotal": "GBP161.00",
                    "saleTaxTotal": "GBP24.93",
                    "saleTotal": "GBP185.93",
                    "passengers": {
                        "kind": "qpxexpress#passengerCounts",
                        "adultCount": 1
                    },
                    "tax": [
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "UB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "UB",
                          "country": "GB",
                          "salePrice": "GBP11.93"
                      },
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "GB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "GB",
                          "country": "GB",
                          "salePrice": "GBP13.00"
                      }
                    ],
                    "fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
                    "latestTicketingTime": "2017-02-25T07:19-05:00",
                    "ptc": "ADT"
                }
              ]
          },
          {
              "kind": "qpxexpress#tripOption",
              "saleTotal": "GBP185.93",
              "id": "GNz9mcDQwhANkULUdCtRhR003",
              "slice": [
                {
                    "kind": "qpxexpress#sliceInfo",
                    "duration": 100,
                    "segment": [
                      {
                          "kind": "qpxexpress#segmentInfo",
                          "duration": 100,
                          "flight": {
                              "carrier": "BA",
                              "number": "1313"
                          },
                          "id": "G9i8bJ8e7SyQdDse",
                          "cabin": "COACH",
                          "bookingCode": "H",
                          "bookingCodeCount": 2,
                          "marriedSegmentGroup": "0",
                          "leg": [
                            {
                                "kind": "qpxexpress#legInfo",
                                "id": "L+53r7MThymrOjmD",
                                "aircraft": "321",
                                "arrivalTime": "2017-02-25T18:50+00:00",
                                "departureTime": "2017-02-25T17:10+00:00",
                                "origin": "ABZ",
                                "destination": "LHR",
                                "destinationTerminal": "5",
                                "duration": 100,
                                "mileage": 401,
                                "meal": "Food and Beverages for Purchase"
                            }
                          ]
                      }
                    ]
                }
              ],
              "pricing": [
                {
                    "kind": "qpxexpress#pricingInfo",
                    "fare": [
                      {
                          "kind": "qpxexpress#fareInfo",
                          "id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "carrier": "BA",
                          "origin": "ABZ",
                          "destination": "LON",
                          "basisCode": "HV2RO"
                      }
                    ],
                    "segmentPricing": [
                      {
                          "kind": "qpxexpress#segmentPricing",
                          "fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "segmentId": "G9i8bJ8e7SyQdDse"
                      }
                    ],
                    "baseFareTotal": "GBP161.00",
                    "saleFareTotal": "GBP161.00",
                    "saleTaxTotal": "GBP24.93",
                    "saleTotal": "GBP185.93",
                    "passengers": {
                        "kind": "qpxexpress#passengerCounts",
                        "adultCount": 1
                    },
                    "tax": [
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "UB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "UB",
                          "country": "GB",
                          "salePrice": "GBP11.93"
                      },
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "GB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "GB",
                          "country": "GB",
                          "salePrice": "GBP13.00"
                      }
                    ],
                    "fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
                    "latestTicketingTime": "2017-02-25T12:09-05:00",
                    "ptc": "ADT"
                }
              ]
          },
          {
              "kind": "qpxexpress#tripOption",
              "saleTotal": "GBP185.93",
              "id": "GNz9mcDQwhANkULUdCtRhR001",
              "slice": [
                {
                    "kind": "qpxexpress#sliceInfo",
                    "duration": 95,
                    "segment": [
                      {
                          "kind": "qpxexpress#segmentInfo",
                          "duration": 95,
                          "flight": {
                              "carrier": "BA",
                              "number": "1307"
                          },
                          "id": "GQZ+5WcC5Xyv4NSo",
                          "cabin": "COACH",
                          "bookingCode": "H",
                          "bookingCodeCount": 1,
                          "marriedSegmentGroup": "0",
                          "leg": [
                            {
                                "kind": "qpxexpress#legInfo",
                                "id": "L4AarejVfUroJVY-",
                                "aircraft": "319",
                                "arrivalTime": "2017-02-25T12:10+00:00",
                                "departureTime": "2017-02-25T10:35+00:00",
                                "origin": "ABZ",
                                "destination": "LHR",
                                "destinationTerminal": "5",
                                "duration": 95,
                                "mileage": 401,
                                "meal": "Food and Beverages for Purchase"
                            }
                          ]
                      }
                    ]
                }
              ],
              "pricing": [
                {
                    "kind": "qpxexpress#pricingInfo",
                    "fare": [
                      {
                          "kind": "qpxexpress#fareInfo",
                          "id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "carrier": "BA",
                          "origin": "ABZ",
                          "destination": "LON",
                          "basisCode": "HV2RO"
                      }
                    ],
                    "segmentPricing": [
                      {
                          "kind": "qpxexpress#segmentPricing",
                          "fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "segmentId": "GQZ+5WcC5Xyv4NSo"
                      }
                    ],
                    "baseFareTotal": "GBP161.00",
                    "saleFareTotal": "GBP161.00",
                    "saleTaxTotal": "GBP24.93",
                    "saleTotal": "GBP185.93",
                    "passengers": {
                        "kind": "qpxexpress#passengerCounts",
                        "adultCount": 1
                    },
                    "tax": [
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "UB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "UB",
                          "country": "GB",
                          "salePrice": "GBP11.93"
                      },
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "GB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "GB",
                          "country": "GB",
                          "salePrice": "GBP13.00"
                      }
                    ],
                    "fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
                    "latestTicketingTime": "2017-02-25T05:34-05:00",
                    "ptc": "ADT"
                }
              ]
           }
        ]
    }
}

I have two foreach loops working to access the airports and prices:

foreach($json['trips']['data']['airport'] as $item) {
    echo "<div>". $item['name'] . "</div>";
}

foreach($json['trips']['tripOption'] as $item) {
    echo "<div>". $item['saleTotal'] . "</div>";
}

The following code does not access the arrival time and I'm unsure why!

foreach($json['trips']['tripOption']['slice']['segment']['leg'] as $item) {
    echo "<div>". $item['arrivalTime'] . "</div>";
}

I have also used the code below to parse my JSON response.

$json = json_decode($results, true);
  • 写回答

1条回答 默认 最新

  • duankuiyuant3940 2017-02-23 14:22
    关注

    Wow that's quiet a dataset to get your head around.

    Here this will print the arrivalTimes

    foreach($json['trips']['tripOption'] as $options) {
        foreach ($options['slice'] as $slices){
            foreach ($slices['segment'] as $segments){
                foreach ($segments['leg'] as $leg ) {
                    echo "<div>". $leg['arrivalTime'] . "</div>
    ";
                }
            }
        }
    }
    

    Result

    <div>2017-02-25T16:15+00:00</div>
    <div>2017-02-25T09:30+00:00</div>
    <div>2017-02-25T14:00+00:00</div>
    <div>2017-02-25T18:50+00:00</div>
    <div>2017-02-25T12:10+00:00</div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler