如果字典中有多个json字段具有相同的名称,如何获取值?

如果字典中有多个json字段具有相同的名称,如何获取值?,json,swift,codable,Json,Swift,Codable,我有一个JSON,响应中有多个字段具有相同的键值名称,我如何访问这些字段?我在swift中使用Codable,我可以从“pricingInfo”中访问第一个“值”,但如何访问其他pricingInfo "fareComponentList":[ { "passengerFareInfoList":[ { "passengerTypeQuantity":{ "passengerType":

我有一个JSON,响应中有多个字段具有相同的键值名称,我如何访问这些字段?我在swift中使用Codable,我可以从“pricingInfo”中访问第一个“值”,但如何访问其他pricingInfo

"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
      };

我该怎么做?(基本上我是想得到成人、儿童和婴儿的价格值)

对于你的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

你需要重构你的json,它是unreadable@Alastar抱歉,已更改使用字典或数组中您自己的自定义结构。如果您已解析JSON并解码模型(因为根对象是列表)然后您只需迭代
passengerFareInfoList
,您的值将位于keypath:
pricingInfo.totalFare.amount.value
@Alladinian,但这只会进入“value”的第一个实例,我可以得到该值,但我想得到Json中的其他“value”字段。(或者如果我错了,你能告诉我正确的方向吗?或者可能是一个示例代码?我复制粘贴了你的整个代码,我使用了未解析的标识符'fareComponentList';你是指'fareComponentList'?这个错误,我替换了它。然后我得到另一个错误“nstance成员'subscript'不能用于'fareComponentList'(也称为'Array')”最后两行只是您的想法。它不是编译代码。如果无法访问下标,请迭代数组并检索值。谢谢,这让我可以访问第一个值。passengerType->ADLT和value->60。但我的问题是如何访问passengerType->CHLD及其值?因为passenger中的所有内容FareInfoList不是数组,因此没有订阅。如何从passengerFareInfoList数组的第二个字典和第三个字典中获取值?