如何在可编码枚举中访问不同的案例?

时间:2019-05-07 08:35:55

标签: json swift codable

我有一个JSON响应,并使用https://app.quicktype.io

将其转换为Codable类

我上了所有结构的课程,有一个特殊情况,

enum Code: String, Codable {
    case adlt = "ADLT"
    case chld = "CHLD"
    case empty = ""
    case inft = "INFT"
    case rq = "RQ"
    case usd = "USD"
}

此枚举代码位于'passengerTypeQuantity(JSON在下面给出)


{
  "passengerTypeQuantity": {
    "passengerType": {
      "code": "ADLT"
    },
    "quantity": 2,
    "hasStrecher": false
  },
  "pricingInfo": {
    "baseFare": {
      "amount": {
        "currency": {
          "code": "USD"
        },
        "value": 60
      }
    },
    "fees": {
      "totalAmount": {
        "currency": {
          "code": "USD"
        },
        "value": 0
      }
    },
    "totalFare": {
      "amount": {
        "accountingSign": "ADC",
        "currency": {
          "code": "USD"
        },
        "value": 87
      }
    },
    "fareConstruction": "",
    "passengerTypeCode": "ADLT",
    "surcharges": {
      "totalAmount": {
        "currency": {
          "code": "USD"
        },
        "value": 24
      },
      "surchargeList": [
        {
          "surchargeAmount": {
            "currency": {
              "code": "USD"
            },
            "value": 24
          },
          "surchargeCode": "YQ",
          "surchargeType": "S",
          "paid": false
        }
      ]
    },
    "taxes": {
      "taxList": [
        {
          "paid": false,
          "taxAmount": {
            "currency": {
              "code": "USD"
            },
            "value": 1
          },
          "taxCode": "AF",
          "taxType": "T"
        },
        {
          "paid": false,
          "taxAmount": {
            "currency": {
              "code": "USD"
            },
            "value": 2
          },
          "taxCode": "M5",
          "taxType": "T"
        }
      ],
      "totalAmount": {
        "currency": {
          "code": "USD"
        },
        "value": 3
      }
    },
    "discountApplied": false,
    "fareBaggageAllowance": 0
  },
  "fareInfoList": [
    {
      "cabinClassCode": "Y",
      "fareReferenceCode": "x",
      "flightSegmentSequence": 1,
      "fareReferenceName": "x",
      "fareGroupName": "x",
      "resBookDesigCode": "S",
      "fareReferenceID": "x"
    }
  ]
}

通常通过Codable,我可以通过以下方式访问其他部分

root.availabilityResultList.first?.availabilityRouteList.first?.availabilityByDateList.first?.originDestinationOptionList.first?.fareComponentGroupList.first?.fareComponentList?.first?.passengerFareInfoList.first?.passengerTypeQuantity.passengerType

这是PassengerFareInfoList结构。

struct PassengerFareInfoList: Codable {
    let passengerTypeQuantity: PassengerTypeQuantity
    let pricingInfo: PricingInfo
    let fareInfoList: [FareInfoList]
}

这是passengerTypeQuantity结构。

struct PassengerTypeQuantity: Codable {
    let passengerType: Currency
    let quantity: Int
    let hasStrecher: Bool
}

这是货币结构

struct Currency: Codable {
    let code: Code
}

来自代码的枚举(如上所述)

enum Code: String, Codable {
    case adlt = "ADLT"
    case chld = "CHLD"
    case empty = ""
    case inft = "INFT"
    case rq = "RQ"
    case usd = "USD"
}

问题:如何检查案件,然后返回定价信息并获取ADLT案件的价格?然后是CHLD案件...等等?

0 个答案:

没有答案