json - 尝试从结构中获取 "variable"但当我尝试时它说结构没有成员 "variable"

标签 json swift decodable

我有一个可编码的结构

struct FareComponentList: Codable {
    let passengerFareInfoList: [PassengerFareInfoList]
    let pricingOverview: PricingOverview
}

我想访问pricingOverview元素。

我尝试了几种方法

1)
root.availabilityResultList.first?.availabilityRouteList.first?.availabilityByDateList.first?.originDestinationOptionList.first?.fareComponentGroupList?.first?. pricingOverview
2)
root.availabilityResultList.first?.availabilityRouteList.first?.availabilityByDateList.first?.originDestinationOptionList.first?.fareComponentGroupList?.[0]. pricingOverview

但它总是返回 nil

然后我尝试了

let x = root.availabilityResultList.first?.availabilityRouteList.first?.availabilityByDateList.first?.originDestinationOptionList.first?.fareComponentGroupList
taxprice.append(x?.fareComponentList)

出现此错误,“[FareComponentGroupList]”类型的值没有成员“fareComponentList”

如何修复此错误/如何从定价概述中获取值?

我在这里做错了什么?

最佳答案

您已将结构声明为“FareComponentList”,其中包含“PricingOverview”。但您正在尝试从不同的名称“FareComponentGroupList”访问“PricingOverview”。

关于json - 尝试从结构中获取 "variable"但当我尝试时它说结构没有成员 "variable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56022162/

相关文章:

c# - JSON 反序列化 : How to get values out of a JSON array of objects

循环中的 javascript 双 JSON 调用

swift - NSMapTable "Generic parameter ' 无法推断出 KeyType”

根据大小写类型,Swift 将枚举返回为字符串或整数

json - "Expected to decode String but found a dictionary instead."

ios - 使用未知的编码键和值进行 Swift 解码

java - JSONObject 不维护顺序

javascript - 当句子中出现某个单词时替换整个字符串 javascript/Google Chrome 扩展

swift - 为什么在具有 Comparable 约束的泛型函数中会丢失泛型类型信息?

ios - 如何在 UIMenu 内的 UIAction 中更改图标颜色?