我有这个JSON,响应中有多个具有相同键值名称的字段,我该如何访问它们呢?我正在快速使用Codable,可以从“ pricingInfo”访问第一个“ value”,但是如何访问其他priceingInfo?
"fareComponentList":[
{
"passengerFareInfoList":[
{
"passengerTypeQuantity":{
"passengerType":{
"code":"CHLD"
},
"quantity":1,
"hasStrecher":false
},
"pricingInfo":{
"baseFare":{
"amount":{
"currency":{
"code":"USD"
},
"value":45
}
},
"fees":{
"totalAmount":{
"currency":{
"code":"USD"
},
"value":0
}
},
"totalFare":{
"amount":{
"accountingSign":"ADC",
"currency":{
"code":"USD"
},
"value":72
}
},
"fareConstruction":"",
"passengerTypeCode":"CHLD",
"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"
}
]
},
{
"passengerTypeQuantity":{
"passengerType":{
"code":"ADLT"
},
"quantity":1,
"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"
}
]
},
{
"passengerTypeQuantity":{
"passengerType":{
"code":"INFT"
},
"quantity":1,
"hasStrecher":false
},
"pricingInfo":{
"baseFare":{
"amount":{
"currency":{
"code":"USD"
},
"value":6
}
},
"fees":{
"totalAmount":{
"currency":{
"code":"USD"
},
"value":0
}
},
"totalFare":{
"amount":{
"accountingSign":"ADC",
"currency":{
"code":"USD"
},
"value":6
}
},
"fareConstruction":"",
"passengerTypeCode":"INFT",
"surcharges":{
"totalAmount":{
"currency":{
"code":"USD"
},
"value":0
}
},
"taxes":{
"totalAmount":{
"currency":{
"code":"USD"
},
"value":0
}
},
"discountApplied":false,
"fareBaggageAllowance":0
},
"fareInfoList":[
{
"cabinClassCode":"Y",
"fareReferenceCode":"x",
"flightSegmentSequence":1,
"fareReferenceName":"x",
"fareGroupName":"x",
"resBookDesigCode":"S",
"fareReferenceID":"x"
}
]
}
],
"pricingOverview":{
"totalTax":{
"accountingSign":"ADC",
"currency":{
"code":"USD"
},
"value":6
},
"totalAmount":{
"accountingSign":"ADC",
"currency":{
"code":"USD"
},
"value":165
},
"totalPenalty":{
"accountingSign":"ADC",
"currency":{
"code":""
},
"value":0
},
"totalBaseFare":{
"accountingSign":"ADC",
"currency":{
"code":"USD"
},
"value":111
},
"equivTotalAmountList":[
{
"accountingSign":"ADC",
"currency":{
"code":""
},
"value":0
}
],
"totalSurcharge":{
"accountingSign":"ADC",
"currency":{
"code":"USD"
},
"value":48
},
"totalDiscount":{
"accountingSign":"ADC",
"currency":{
"code":""
},
"value":0
},
"totalServiceCharge":{
"accountingSign":"ADC",
"currency":{
"code":""
},
"value":0
},
"totalOtherFee":{
"accountingSign":"ADC",
"currency":{
"code":""
},
"value":0
}
}
}
]
我想做的是,存储每个
中的“值”"pricingInfo":{
"baseFare":{
"amount":{
"currency":{
"code":"USD"
},
"value":45
};
我该怎么办?(基本上是在尝试获取成人,儿童和婴儿的价格)
答案 0 :(得分:1)
对于您的JSON,这可以是您的可编码模型。
typealias FareComponentList = [FareComponentListElement]
struct FareComponentListElement: Codable {
let passengerFareInfoList: [PassengerFareInfoList]
let pricingOverview: PricingOverview
}
struct PassengerFareInfoList: Codable {
let passengerTypeQuantity: PassengerTypeQuantity
let pricingInfo: PricingInfo
let fareInfoList: [FareInfoList]
}
struct FareInfoList: Codable {
let cabinClassCode, fareReferenceCode: String
let flightSegmentSequence: Int
let fareReferenceName, fareGroupName, resBookDesigCode, fareReferenceID: String
}
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 usd = "USD"
}
struct PricingInfo: Codable {
let baseFare: BaseFare
let fees: Fees
let totalFare: TotalFare
let fareConstruction: String
let passengerTypeCode: Code
let surcharges: Surcharges
let taxes: Taxes
let discountApplied: Bool
let fareBaggageAllowance: Int
}
struct BaseFare: Codable {
let amount: Amount
}
struct Amount: Codable {
let currency: Currency
let value: Int
}
struct Fees: Codable {
let totalAmount: Amount
}
struct Surcharges: Codable {
let totalAmount: Amount
let surchargeList: [SurchargeList]?
}
struct SurchargeList: Codable {
let surchargeAmount: Amount
let surchargeCode, surchargeType: String
let paid: Bool
}
struct Taxes: Codable {
let taxList: [TaxList]?
let totalAmount: Amount
}
struct TaxList: Codable {
let paid: Bool
let taxAmount: Amount
let taxCode, taxType: String
}
struct TotalFare: Codable {
let amount: TotalAmount
}
struct TotalAmount: Codable {
let accountingSign: AccountingSign
let currency: Currency
let value: Int
}
enum AccountingSign: String, Codable {
case adc = "ADC"
}
struct PricingOverview: Codable {
let totalTax, totalAmount, totalPenalty, totalBaseFare: TotalAmount
let equivTotalAmountList: [TotalAmount]
let totalSurcharge, totalDiscount, totalServiceCharge, totalOtherFee: TotalAmount
}
使用以上模型,可以访问以下值。
let passengerType = fareComponentList[0].passengerFareInfoList[0].passengerTypeQuantity.passengerType // ADLT / CHLD / INFT
let amount = fareComponentList[0].passengerFareInfoList[0].pricingInfo.baseFare.amount.value