从嵌套的json中提取数据

时间:2015-04-23 03:56:04

标签: php json

我有一个嵌套的JSON代码,如下所示:

  fn({
    "processingDurationMillis": 714,
    "authorisedAPI": true,
    "success": true,
    "airline": "MH",
    "validCreditCards": [
        "AX",
        "CA",
        "VI"
    ],
    "paypal": true,
    "outboundOptions": [
        {
            "optionId": 0,
            "flights": [
                {
                    "flightNumber": "0066",
                    "departureAirport": {
                        "code": "KUL",
                        "name": "Kuala Lumpur Intl",
                        "city": "Kuala Lumpur",
                        "country": "Malaysia",
                        "timezone": "Asia/Kuala_Lumpur",
                        "lat": 2.745578,
                        "lng": 101.709917,
                        "terminal": null,
                        "gate": null
                    },
                    "arrivalAirport": {
                        "code": "ICN",
                        "name": "Incheon Intl",
                        "city": "Seoul",
                        "country": "South Korea",
                        "timezone": "Asia/Seoul",
                        "lat": 37.469075,
                        "lng": 126.450517,
                        "terminal": null,
                        "gate": null
                    },
                    "marketingAirline": "MH",
                    "mealIndicator": "M",
                    "allowedSsrs": {

                    },
                    "operatingAirline": null,
                    "equipment": "333",
                    "equipmentName": "Airbus A330-300",
                    "flightRPH": 10101,
                    "comments": [
                        "MH CODESHARE WITH KE"
                    ],
                    "depScheduled": "2015-04-28T23:30:00.000+08:00",
                    "arrScheduled": "2015-04-29T07:10:00.000+09:00",
                    "depEstimated": null,
                    "depActual": null,
                    "arrEstimated": null,
                    "arrActual": null,
                    "eligibleForEticketing": true,
                    "cabin": "ECONOMY",
                    "fareMarketingType": "LOWEST",
                    "rbd": "O",
                    "seatsAvailable": 2,
                    "durationMinutes": 400,
                    "minutesToScheduledFlightDeparture": 7877
                }
            ],
            "stopOvers": [

            ],
            "fareDetails": {
                "perPassengerJourneyFares": [
                    {
                        "passengerType": "ADT",
                        "fare": "852.93",
                        "currencyCode": "MYR"
                    }
                ],
                "perPassengerTripTaxes": [
                    {
                        "passengerType": "ADT",
                        "totalTax": "68.90",
                        "taxes": [
                            {
                                "code": "MY",
                                "amount": "65.00",
                                "currency": "MYR"
                            },
                            {
                                "code": "D8",
                                "amount": "3.90",
                                "currency": "MYR"
                            }
                        ]
                    }
                ],
                "journeyFare": "852.93",
                "totalTripFare": "921.90",
                "fareCurrency": "MYR"
            },
            "magicString": "mBkCLcD63tlNguPp1ZhbqE99F5uY2SahL3rnMrgD5/Bylh3q2u2UfDCV5DaiQ/Okp6VkdUvBjnUrsiHiL2T/E9xTWTFoSQBw18t2HPQI6KAPGlWNDjDXl8Tiz63YtQ+/WRX3RSJ62KM5azSj6piLWeBN8wvZy1b8dJssMw8dWNkmCwjNxbGor0mepeewSu8ebiYtoHfaDmcd1xdk+kczn2PMjXxUVdFgvXJNnDOke71H/cw5Njh2bAetCQv+fqluAQfmKutZczIR6hJG2u+kJSLaVeUfpR9RNMeN3VvLUZNbLE0Bxe0UtbEHnSLRBRVE7WfDeofOg7iiIYKy42Z1l4T8CIvy92x/vVHoe2oOpONS7xH8YlBG86BOs1eNeFTQOZ3uMzExOxrsJl/DaQOAW3hrJ4Mv4IiSuwli69r8wHbT/iw3JYEGDPnsZA81W4KJJl4bkAMuN7PD4DygdGlRFKYPWcyyYW+wlFge8v//d+ndyrMVzQOh9HPq7nLv8lWPIKK/zzKjSJGv4jtNrNuvyP5/OXzS392P8tZLEKgwuq3LSnZX3P+mSvnFU3LgL+6VjrPLkiAC9pzEF+NnRYzGKp+Hv8nAFhzEOGuxXwXkYAzcFwHlnRluJ894K7GbP8dwRNE52NGzufxrS77DAtVCc1KaW4ZfJ920N126DP05JvRkL6TOepG7MDhV1sCL72P7sICTOR+ROAYKmAedI9lpEBkMtja/siHb581eHZ15m/Tp9VFD8Q62wDFgzUb6fMD8/5yXK/gt4BCWOcbxNqD3nva8JQ2pVZu22JJFg+SlGkVLxTAplQx4HyRXryUjXL4kUais+UVx0tBBWDIuVpoZGdEIUA/eQ8xBXvj8fusswCc5ut3pmykk50Hu0TXhv40iIjP9CtclqyH13Xl6dYJ4n24utUgrhyn42UorGKJLimtxLlj8Wwf7QFNMVPnLrkAfqmJ/80YaF/i43H3IxuueFPIpC2QSAUN/5KtB3HSBKHUgb/cQgJlPtBrPvrjjsaiCmz9HEdGbnWryMnq7mxrPmeDEHnFYL4WY8gu0N75QalXsbAwEhLv0RunX1Krk04o6p1uf20wEtPGXSjPJrofpJLlOWAr019mGfXnBvodVQbB5sMDVlD3JFxysw4+9KElFcF+FFzFZZDxUUWAMG5p6U+HIoeC13mMZ63hpU+KZNa/0meLTSGMxce919qRyEt1DxU4Ty3u5fJ5NJc028NF6LLpDHs7uNok8AkI3yDaLGsLT12rNqjLgOelHjTQxwX5ez1JjUjalPm00sgcq4RhTsOQaOAIqHf4mq1zNES3uyiWSK0MmGugxvGZ1RDAq9G/E",
            "seatsAvailable": [
                2
            ],
            "corporateAccount": false,
            "flightCanBeHeld": true,
            "durationMinutes": 400,
            "gaFareDetails": {
                "perPassengerJourneyFares": [
                    {
                        "passengerType": "ADT",
                        "fare": "852.93",
                        "currencyCode": "MYR"
                    }
                ],
                "perPassengerTripTaxes": [
                    {
                        "passengerType": "ADT",
                        "totalTax": "68.90",
                        "taxes": [
                            {
                                "code": "MY",
                                "amount": "65.00",
                                "currency": "MYR"
                            },
                            {
                                "code": "D8",
                                "amount": "3.90",
                                "currency": "MYR"
                            }
                        ]
                    }
                ],
                "journeyFare": "852.93",
                "totalTripFare": "921.90",
                "fareCurrency": "MYR"
            },
            "adobeFareDetails": {
                "perPassengerJourneyFares": [
                    {
                        "passengerType": "ADT",
                        "fare": "234.81",
                        "currencyCode": "USD"
                    }
                ],
                "perPassengerTripTaxes": [
                    {
                        "passengerType": "ADT",
                        "totalTax": "18.97",
                        "taxes": [
                            {
                                "code": "MY",
                                "amount": "17.89",
                                "currency": "USD"
                            },
                            {
                                "code": "D8",
                                "amount": "1.07",
                                "currency": "USD"
                            }
                        ]
                    }
                ],
                "journeyFare": "234.81",
                "totalTripFare": "253.80",
                "fareCurrency": "USD"
            },
            "userAgentFareDetails": {
                "perPassengerJourneyFares": [
                    {
                        "passengerType": "ADT",
                        "fare": "234.81",
                        "currencyCode": "USD"
                    }
                ],
                "perPassengerTripTaxes": [
                    {
                        "passengerType": "ADT",
                        "totalTax": "18.97",
                        "taxes": [
                            {
                                "code": "MY",
                                "amount": "17.89",
                                "currency": "USD"
                            },
                            {
                                "code": "D8",
                                "amount": "1.07",
                                "currency": "USD"
                            }
                        ]
                    }
                ],
                "journeyFare": "234.81",
                "totalTripFare": "253.80",
                "fareCurrency": "USD"
            },
            "directFlight": true,
            "eligibleForeTicketing": true,
            "lowestSeatCount": 2
        }
    ],
    "departureAirport": {
        "code": "KUL",
        "name": "Kuala Lumpur Intl",
        "city": "Kuala Lumpur",
        "country": "Malaysia",
        "timezone": "Asia/Kuala_Lumpur",
        "lat": 2.745578,
        "lng": 101.709917,
        "terminal": null,
        "gate": null
    },
    "arrivalAirport": {
        "code": "ICN",
        "name": "Incheon Intl",
        "city": "Seoul",
        "country": "South Korea",
        "timezone": "Asia/Seoul",
        "lat": 37.469075,
        "lng": 126.450517,
        "terminal": null,
        "gate": null
    },
    "apiRequired": true,
    "fareRules": [
        {
            "id": 40,
            "order": 0,
            "priority": 0,
            "code": "Lowest",
            "name": "MHlow",
            "value": "Economy Class Fares",
            "listFareRules": [
                {
                    "id": 10,
                    "order": 0,
                    "code": "",
                    "name": "Discount level",
                    "value": "Up to 80%"
                },
                {
                    "id": 20,
                    "order": 1,
                    "code": "",
                    "name": "Where to buy",
                    "value": "All channels"
                },
                {
                    "id": 30,
                    "order": 2,
                    "code": "",
                    "name": "Advance purchase",
                    "value": "Applies"
                },
                {
                    "id": 40,
                    "order": 3,
                    "code": "",
                    "name": "Payment",
                    "value": "Ticket dateline applies"
                },
                {
                    "id": 50,
                    "order": 4,
                    "code": "",
                    "name": "Baggage allowance",
                    "value": "2pc/30kg"
                },
                {
                    "id": 60,
                    "order": 5,
                    "code": "",
                    "name": "Advance seat selection",
                    "value": "Not allowed"
                },
                {
                    "id": 70,
                    "order": 6,
                    "code": "",
                    "name": "Enrich miles",
                    "value": "Nil"
                },
                {
                    "id": 80,
                    "order": 7,
                    "code": "",
                    "name": "Change of booking",
                    "value": "Not allowed"
                },
                {
                    "id": 90,
                    "order": 8,
                    "code": "",
                    "name": "Upgrade",
                    "value": "Not allowed"
                },
                {
                    "id": 100,
                    "order": 9,
                    "code": "",
                    "name": "Stand by at the airport",
                    "value": "Not allowed"
                },
                {
                    "id": 110,
                    "order": 10,
                    "code": "",
                    "name": "No show",
                    "value": "Penalty applies"
                },
                {
                    "id": 120,
                    "order": 11,
                    "code": "",
                    "name": "Refund",
                    "value": "No Refund"
                }
            ],
            "listFareNotes": [
                {
                    "id": 10,
                    "order": 0,
                    "code": "",
                    "name": "Important Notice",
                    "value": ""
                },
                {
                    "id": 15,
                    "order": 1,
                    "code": "",
                    "name": "",
                    "value": ""
                },
                {
                    "id": 20,
                    "order": 2,
                    "code": "",
                    "name": "1.",
                    "value": "Generic attributes shown only applies to MH operated flights. MH3000-3999, MH5200-5999 and MH9000-9999 Series flights are subject to their own rules. Please contact MH Call Center or ticket offices for actual fare rules."
                },
                {
                    "id": 30,
                    "order": 3,
                    "code": "",
                    "name": "2.",
                    "value": "For transpacific and transatlantic flights, the following baggage allowances apply: Economy - 2 pieces (23kg each piece), First and Business - 2 pieces (32kg each piece)."
                },
                {
                    "id": 50,
                    "order": 5,
                    "code": "",
                    "name": "3.",
                    "value": "Upgrade, standby at the airport and refund fees for specific routes can be obtained from subsequent booking pages."
                },
                {
                    "id": 60,
                    "order": 6,
                    "code": "",
                    "name": "4.",
                    "value": "Standby at the airport denotes the same day for an earlier flight."
                },
                {
                    "id": 70,
                    "order": 7,
                    "code": "",
                    "name": "5.",
                    "value": "Fare rules shown are indicative only. Please call our Contact Center to check the detailed fare rules."
                },
                {
                    "id": 80,
                    "order": 8,
                    "code": "",
                    "name": "",
                    "value": "Should there be any discrepancy between the above information and the terms and conditions (T&C) published in the fare rules, then the T&C in the fare rules shall prevail."
                }
            ]
        }
    ],
    "Errors": [

    ],
    "Warnings": [

    ]
})

当我试图从上面的json获得航班号时,它会给我一个错误。"试图获得非对象的属性"和#34;为foreach()"提供的参数无效。

这是我的代码:

$flight = json_decode($result,true);
$info = $flight->outboundOptions->flights;
foreach ($info as $flightinfo){

        echo $flightinfo['flightNumber'];
}

我知道这是错的,但我不确定我是否在正确的轨道上。

提前致谢。

1 个答案:

答案 0 :(得分:0)

通过将true指定为json_decode()的第二个参数,您告诉它返回关联数组而不是对象,因此您可以将其取出以匹配当前代码。

另请注意$flight->outboundOptions是一个数组,因此您需要循环它或访问[0]索引才能到达->flights

试试这个:

$flight = json_decode($result);
$info = $flight->outboundOptions[0]->flights;
foreach ($info as $flightinfo){
    echo $flightinfo->flightNumber;
}