需要一些帮助才能获得JSON的值.我想访问radioButton对象,并访问description值(例如,竞争对手的其他产品有更好的产品),以分配给tileView.title,如下所示:
for viewContent in viewModel.contents {
switch viewContent {
case let .radioButtonGroup(radioButtonGroupModel):
for i in 0..<radioButtonGroupModel.options.count {
let tileView = TileRadio()
tileView.title = radioButtonGroupModel.options...
打印此print(radioButtonGroupModel.options[i])将获得:
radioButton(RadioButtonModel(description: "Other Products from Competitors have better product", identifier: "competitorReason"))
以下是JSON解码器的代码库:
enum ComponentModel: Decodable {
case radioButtonGroup(RadioButtonGroupModel)
case radioButton(RadioButtonModel)
case emptyComponent
enum CodingKeys: String, CodingKey {
case radioButtonGroup
case radioButton
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do {
switch container.allKeys.first {
case .radioButtonGroup:
let value = try container.decode(RadioButtonGroupModel.self, forKey: .radioButtonGroup)
self = .radioButtonGroup(value)
case .radioButton:
let value = try container.decode(RadioButtonModel.self, forKey: .radioButton)
self = .radioButton(value)
case .none:
self = .emptyComponent
}
} catch {
self = .emptyComponent
}
}
}
struct RadioButtonGroupModel: Decodable {
let options: [ComponentModel]
}
struct RadioButtonModel: Decodable {
let description: String
let identifier: String
}
以下是部分JSON文件:
"closureReasons": {
"pageTitle": "Close account",
"content": [
{
"heading": {
"title": "Why are you closing this account?"
}
},
{
"spacing": ".spacing3x"
},
{
"radioButtonGroup": {
"options": [
{
"radioButton": {
"identifier": "competitorReason",
"description": "Other Products from Competitors have better product"
}
},
{
"radioButton": {
"identifier": "betterReason",
"description": "I found a better product"
}
},
你对如何检索单选按钮属性有什么 idea 吗?非常感谢您的帮助