dongqian1028 2015-04-23 03:56
浏览 344

从嵌套的json中提取数据

I've a nested JSON code as below:

  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": [

    ]
})

when i trying to get flight number from the json above, it return me a error."Trying to get property of non-object" and "Invalid argument supplied for foreach()".

here is my code:

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

        echo $flightinfo['flightNumber'];
}

I know this is wrong, but I'm not sure if I'm on the right track or not.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongnong3019 2015-04-23 04:09
    关注

    By specifying true as the second parameter of json_decode() you are telling it to return an associative array rather than an object, so you could take that out to match your current code.

    Also be aware that $flight->outboundOptions is an array, so you'll need to loop it or access the [0] index before you can get to ->flights.

    Try this:

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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集