List to display data from server in swiftUI

Im fetching data from server and I need to show it like displayed in this site SwiftUI – nested list

Data from server is

  "OptionsListByItemId": [
        {
         "Choices": [
           {
             "ChoiceId": 1284226,
              "ChoiceName": "Hot",
            },
            { 
              "ChoiceId": 1284227,
              "ChoiceName": "Cool",
            }
          ],
          "OptionId": 187472,
          "OptionName": "Temperature"
      },
    
      { 
       "Choices": [
           {
             "ChoiceId": 1284223,
              "ChoiceName": "61%",
            },
            { 
              "ChoiceId": 1284224,
              "ChoiceName": "70%",
            }
          ],
          "OptionId": 187473,
          "OptionName": "Humidity"
 ]
}

My model is like

struct OptionsandChoices : Decodable , Identifiable{
    
    var id: String{OptionName}
    var OptionName: String!
    var OptionId: Int
    var Choices : [ChoiseList]
    
}

struct OptionsandChoiceList: Decodable{
    
    var OptionsListByItemId:[OptionsandChoices]  
}
struct ChoiseList: Decodable {
    var ChoiceName: String!
    var ChoiceId: Int
}

ViewModel is

class ItemChoiceViewModel : ObservableObject{
 @Published var OpnChoice: OptionsandChoiceList = OptionsandChoiceList(OptionsListByItemId: [])
 // fetching data from server 
}

My swiftUI view like

struct ItemView: View {
 var OpnChoice = OptionsandChoiceList(OptionsListByItemId: [])
 @ObservedObject var choicevwModel = ChoiceViewModel()

struct Optionspage: View {
   var body: some View {
   List(choicevwModel.OpnChoice.OptionsListByItemId) {opn in 
       Text(opn.OptionName)
   }
}

Im unable to use ChoiceName in List

How can I able to get choiceName in each row below OptionName like in the link I gave

List Should be displayed like

 Temperature 
    Hot 
    Cold 
 
 Humidity
    61%
    70%

Currently I get in two rows

 Temperature
 Humidity

Answer

The property you are looking for is located in Choices. To get it’s value write following:

opn.Choices[yourIndex].OptionName

Also, if you want to use @ObservedObject make your OptionsandChoiceList class instead of struct and make it conform ObservableObject protocol. Finally, declare @Published property there.

final class OptionsandChoiceList: ObservableObject, Decodable {
    @Published var OptionsListByItemId: OptionsandChoices = []  
}