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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。