簡體   English   中英

從嵌套的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": [

]

})

我想從上面的json中提取航班號,depScheduled,arrScheduled和旅程票價。

這是我的代碼:

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

上面的代碼給我一個錯誤,“為foreach()提供了無效的參數”。我搜索過,但仍然沒有找到解決方案..

它給出了這個錯誤,因為outboundOptions是一個對象數組。 你想要的是訪問第一個對象:

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

此外,刪除尾隨逗號( ,從你) ]在端部作為使無效json

您可以訪問jsonlint.com查看您的json是否有效

工作實例


根據您的評論更新

要獲得所有航班,請將您的foreach循環更改為:

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

  1. 刪除json末端附近的尾隨','。

  2. 更改您的代碼,在['flights']之前添加[0] ['flights']

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM