如何从带有多个数组和括号的大型JSON响应中获取值?

时间:2019-05-06 07:17:27

标签: ios json swift alamofire swifty-json

我有一个JSON响应(超过500行),其中包含多个数组和括号。基本上是过于嵌套的响应。

我正在使用Alamofire进行提取,并且得到了JSONData作为响应。但是我不知道如何从响应中获取值并将其打印在自定义单元格上。

我正在使用Alamofire 5来获取JSON响应。

如果这听起来太新了,请原谅,我是iOS开发的新手。

{
    "availabilityResultList": [
        {
            "availabilityRouteList": [
                {
                    "availabilityByDateList": [
                        {
                            "originDestinationOptionList": [
                                {
                                    "fareComponentGroupList": [
                                        {
                                            "boundList": [
                                                {
                                                    "availFlightSegmentList": [
                                                        {
                                                            "addOnSegment": false,
                                                            "flightSegment": {
                                                                "journeyDuration": "PT1H15M",
                                                                "distance": 1,
                                                                "departureAirport": {
                                                                    "locationName": "LOC",
                                                                    "cityInfo": {
                                                                        "country": {
                                                                            "locationName": "LOC",
                                                                            "currency": {
                                                                                "code": "USD"
                                                                            },
                                                                            "locationCode": "LOC",
                                                                            "locationNameLanguage": "EN"
                                                                        },
                                                                        "city": {
                                                                            "locationName": "LOC",
                                                                            "locationCode": "LOC",
                                                                            "locationNameLanguage": "EN"
                                                                        }
                                                                    },
                                                                    "codeContext": "ASD",
                                                                    "language": "EN",
                                                                    "locationCode": "LOC"
                                                                },
                                                                "equipment": {
                                                                    "airEquipType": "ABC",
                                                                    "changeofGauge": false
                                                                },
                                                                "remark": "NO",
                                                                "ticketType": "PP",
                                                                "onTimeRate": 0,
                                                                "flightNotes": [
                                                                    {
                                                                        "deiCode": 0
                                                                    }
                                                                ],
                                                                "flightSegmentID": "",
                                                                "flownMileageQty": 0,
                                                                "flightNumber": "106",
                                                                "groundDuration": "",
                                                                "trafficRestriction": {
                                                                    "code": "",
                                                                    "explanation": {}
                                                                },
                                                                "stopQuantity": 0,
                                                                "codeshare": false,
                                                                "secureFlightDataRequired": false,
                                                                "departureDateTime": {
                                                                    "month": 5,
                                                                    "hour": 6,
                                                                    "year": 2019,
                                                                    "timezone": 270,
                                                                    "day": 6,
                                                                    "minute": 45,
                                                                    "second": 0
                                                                },
                                                                "ondControlled": false,
                                                                "arrivalDateTime": {
                                                                    "month": 5,
                                                                    "hour": 8,
                                                                    "year": 2019,
                                                                    "timezone": 270,
                                                                    "day": 6,
                                                                    "minute": 0,
                                                                    "second": 0
                                                                },
                                                                "airline": {
                                                                    "code": "RQ"
                                                                },
                                                                "arrivalAirport": {
                                                                    "locationName": "LOC",
                                                                    "cityInfo": {
                                                                        "country": {
                                                                            "locationName": "AA",
                                                                            "currency": {
                                                                                "code": "USD"
                                                                            },
                                                                            "locationCode": "AA",
                                                                            "locationNameLanguage": "EN"
                                                                        },
                                                                        "city": {
                                                                            "locationName": "loc",
                                                                            "locationCode": "LOC",
                                                                            "locationNameLanguage": "EN"
                                                                        }
                                                                    },
                                                                    "codeContext": "ASD",
                                                                    "language": "EN",
                                                                    "locationCode": "LOC"
                                                                },
                                                                "sector": "ASD",
                                                                "accumulatedDuration": ""
                                                            },
                                                            "marriageGroup": "-1"
                                                        }
                                                    ],
                                                    "boundCode": "Outbound"
                                                }
                                            ]
                                        }
                                    ]
                                },

1 个答案:

答案 0 :(得分:4)

JSON是一种非常简单的格式,易于阅读。只有两种不同的集合类型和四种值类型。

  • {}是一本字典,并用键下标
  • []是一个数组,并由索引(从零开始)下标

  • 所有双引号都是String,甚至是"123""false"
  • falsetrueBool
  • 数值为Double,不带小数位数Int
  • <null>NSNull

使用SwiftyJSON,您可以在distance中获得flightSegment,其中(root代表顶级对象`)

root["availabilityResultList"][0]["availabilityRouteList"][0]["availabilityByDateList"][0]["originDestinationOptionList"][0]["fareComponentGroupList"][0]["boundList"][0]["availFlightSegmentList"][0]["flightSegment"]["distance"]