从嵌套 JSON 中提取数据值

extract data value from nested JSON

我正试图从这个 Json 中获取一些特定的字段。

 fn({
"processingDurationMillis": 454,
"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": "BASIC",
                "rbd": "N",
                "seatsAvailable": 9,
                "durationMinutes": 400,
                "minutesToScheduledFlightDeparture": 6486
            }
        ],
        "stopOvers": [

        ],
        "fareDetails": {
            "perPassengerJourneyFares": [
                {
                    "passengerType": "ADT",
                    "fare": "1214.95",
                    "currencyCode": "MYR"
                }
            ],
            "perPassengerTripTaxes": [
                {
                    "passengerType": "ADT",
                    "totalTax": "68.90",
                    "taxes": [
                        {
                            "code": "MY",
                            "amount": "65.00",
                            "currency": "MYR"
                        },
                        {
                            "code": "D8",
                            "amount": "3.90",
                            "currency": "MYR"
                        }
                    ]
                }
            ],
            "journeyFare": "1214.95",
            "totalTripFare": "1283.90",
            "fareCurrency": "MYR"
        },
        "magicString": "2t2qi8oNXWkrDR75zYDrk9+3wNaJBzHyK1ftoR/VZPVgHO+EFTkh8DMg5WUl1ap7VjwBsnhD2gFxAwBbHhY0+k0lp7BUvSoYSKg6S6u4ZkvbIWMktl+lHgcKl46vht9//2dZVJvH4D7WJvnJTtK5O4TWNrkiTmEdHp55yRmjwWfsgNswOIMXoWrZj3OUJ4DH4POJ8rmfilimvtpBCdxNsqoZDVC9d6/6LiICZ3wHZJ7w/88QuExFV7OsHbc+jI3trRzDCCb6Ns62MGyfsXX6Pz8mJe6gs02UjapVSPa3M9CqLGMCN0xCF28WNbavhSI9jG3cWsQbxGU8rnhmjx00Iw5v2qqjdE/Dx432Qzs4s36SqUjLF7KN9hAJoQuMX3emE4gZ+7ANJ5bDTDEYZlnUZ4iXKykzUptYDyGay0evu1kdCjxPJlgiEtOl3hFMaKC+eoTsjps4RoYy0Z7oD3aP52qCYPdCH+8XTic522UKU1mW9HMjmGxH5zrvYK2rOgzSR2+xH5K3IpXHBAQqWOTEvmirP4qvg5VOPjyO9mIM83I6aY1JAkqo9jYqtEwrGqANdhA9z78EdoyQYKZBXcLsQMKz06fAczwk/WxxIi1ctL8EW+aZYddkbPo7xD6NWc8bJ+ARw5AlS1tirVNcO3mN5jVr/a6qftVuaz/0q83VsX4ztQpgMjDkptbw9Zz6DNLgiLJEzdf7fraoVUyzeth5wucOMzpLBP+ERbD7XFnDSKN8QzG6lLpDK8qy95K5FMmcF4uDq8Y1waTyIN9sS+v50OTbjr7Ebs3uKIxMZFfGUfp7YpDiVyo+2x4La4K7rhHPtoR6iEfVCjnTAUvamQu3qgL3vuSCPPPJiHFbdOrKVlp3kfAxaIcJpX3Z+Twx2cNAhsGHSk2ZazzvP5Pw1EF066VcoDkld9Oe/Qu5cC+DtG2LHhMA7NU8hMD66q9UCsXC6P/mjbKr7hatjHyyklDIKuxxirMpYkukEa73RJlhKmC0fjj4EYcgRy5MtybexuN59KaTeSEFxMGFIkv0zHp5jO/wHUvyypqbxTKFR3VAx6WpmSNg/Iui2uXDhNu/F4zJnYQUW9EyluZEPebFk2Uj455O2+y0UmFe4WnUY+0d92obZNv855/ctA4UC/LQn2s9azqdhDIeUUHuHEn2a4Grb+7l8wuai6ybBmmE62ck+CqMou+A+CUwk71KMkh3ZXf8BdeelW8Ia7r9ja7wKNBklgYo4Q8xOR63QhyCt2BiiR9aOxiDIKiW7bxSFCBga7yIPWx/NZdGjUYTuiJ9KZ7W2dKLhF6XDU5mWOV7XwMRzkyschEnjSzQWGjTTftEIiNI1V1M2bhFwc92JkfVFxwXCg==",
        "seatsAvailable": [
            9
        ],
        "corporateAccount": false,
        "flightCanBeHeld": true,
        "durationMinutes": 400,
        "gaFareDetails": {
            "perPassengerJourneyFares": [
                {
                    "passengerType": "ADT",
                    "fare": "1214.95",
                    "currencyCode": "MYR"
                }
            ],
            "perPassengerTripTaxes": [
                {
                    "passengerType": "ADT",
                    "totalTax": "68.90",
                    "taxes": [
                        {
                            "code": "MY",
                            "amount": "65.00",
                            "currency": "MYR"
                        },
                        {
                            "code": "D8",
                            "amount": "3.90",
                            "currency": "MYR"
                        }
                    ]
                }
            ],
            "journeyFare": "1214.95",
            "totalTripFare": "1283.90",
            "fareCurrency": "MYR"
        },
        "adobeFareDetails": {
            "perPassengerJourneyFares": [
                {
                    "passengerType": "ADT",
                    "fare": "336.66",
                    "currencyCode": "USD"
                }
            ],
            "perPassengerTripTaxes": [
                {
                    "passengerType": "ADT",
                    "totalTax": "19.09",
                    "taxes": [
                        {
                            "code": "MY",
                            "amount": "18.01",
                            "currency": "USD"
                        },
                        {
                            "code": "D8",
                            "amount": "1.08",
                            "currency": "USD"
                        }
                    ]
                }
            ],
            "journeyFare": "336.66",
            "totalTripFare": "355.77",
            "fareCurrency": "USD"
        },
        "userAgentFareDetails": {
            "perPassengerJourneyFares": [
                {
                    "passengerType": "ADT",
                    "fare": "336.66",
                    "currencyCode": "USD"
                }
            ],
            "perPassengerTripTaxes": [
                {
                    "passengerType": "ADT",
                    "totalTax": "19.09",
                    "taxes": [
                        {
                            "code": "MY",
                            "amount": "18.01",
                            "currency": "USD"
                        },
                        {
                            "code": "D8",
                            "amount": "1.08",
                            "currency": "USD"
                        }
                    ]
                }
            ],
            "journeyFare": "336.66",
            "totalTripFare": "355.77",
            "fareCurrency": "USD"
        },
        "eligibleForeTicketing": true,
        "lowestSeatCount": 9,
        "directFlight": true
    }
],
"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": 50,
        "order": 1,
        "priority": 0,
        "code": "Basic",
        "name": "MHbasic",
        "value": "Economy Class Fares",
        "listFareRules": [
            {
                "id": 130,
                "order": 0,
                "code": "",
                "name": "Discount level",
                "value": "Up to 65%"
            },
            {
                "id": 140,
                "order": 1,
                "code": "",
                "name": "Where to buy",
                "value": "All channels"
            },
            {
                "id": 150,
                "order": 2,
                "code": "",
                "name": "Advance purchase",
                "value": "Applies"
            },
            {
                "id": 160,
                "order": 3,
                "code": "",
                "name": "Payment",
                "value": "Ticket dateline applies"
            },
            {
                "id": 170,
                "order": 4,
                "code": "",
                "name": "Baggage allowance",
                "value": "2pc/30kg"
            },
            {
                "id": 180,
                "order": 5,
                "code": "",
                "name": "Advance seat selection",
                "value": "Not allowed"
            },
            {
                "id": 190,
                "order": 6,
                "code": "",
                "name": "Enrich miles",
                "value": "Nil"
            },
            {
                "id": 200,
                "order": 7,
                "code": "",
                "name": "Change of booking",
                "value": "Not allowed"
            },
            {
                "id": 210,
                "order": 8,
                "code": "",
                "name": "Upgrade",
                "value": "Not allowed"
            },
            {
                "id": 220,
                "order": 9,
                "code": "",
                "name": "Stand by at the airport",
                "value": "For a fee"
            },
            {
                "id": 220,
                "order": 10,
                "code": "",
                "name": "No show",
                "value": "Penalty applies"
            },
            {
                "id": 230,
                "order": 11,
                "code": "",
                "name": "Refund",
                "value": "For a fee"
            }
        ],
        "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": [

]

})

我想从上面提取航班号、depScheduled、arrScheduled 和旅程票价 json。

这是我的代码:

$json2 = json_decode($json,true);
$result= array();
foreach ($json2['outboundOptions']['flights']as $theentity) {
    $result[] = $theentity['flightNumber'];
}
print_r($result);

上面的代码return我错了,"Invalid argument supplied for foreach()"。我四处寻找,但仍然没有找到解决方案..

它给你这个错误是因为 outboundOptions 是一个对象数组。你想要的是访问第一个对象:

foreach ($json2['outboundOptions'][0]['flights']as $theentity) {
    $result[] = $theentity['flightNumber'];
}

此外,从您的 ] 末尾删除尾随逗号 (,),因为这会导致 json 无效。

您可以前往 jsonlint.com

检查您的 json 是否有效

Working Example


根据您的评论更新

要获取所有航班,请将 foreach 循环更改为:

foreach ($json3['outboundOptions'] as $flight) {
  foreach($flight['flights'] as $theentity) {
    $result[] = $theentity['flightNumber'];
  }
}

Example

  1. 删除 json.

  2. 末尾的尾随 ','
  3. 更改您的代码,在 ['flights']

    之前添加一个 [0]

    $json2 = json_decode($json,TRUE); $result= array(); foreach ($json2['outboundOptions'][0]['flights']as $theentity) { $result[] = $theentity['flightNumber']; } print_r($result);