Swift 3/JSON - How to capture List of objects inside an object?

Asked

Viewed 409 times

1

How do I access a list of objects within another list of objects?
I can capture these objects peacefully, but I’m having trouble capturing objects within this list.
This is my JSON:

JSON

In this case I want to have access to the values that are within "NOTES" and "MISSING" but I’m not having much success in this...
Follows my code:

 for item in resultado!{

    let newDisc = DisciplinasML()

    newDisc.codDisciplina = item["COD_DISCIPLINA"]! as? String
    newDisc.desDisciplina = item["DES_DISCIPLINA"]! as? String
    newDisc.desPeriodo = item["DES_PERIODO"]! as? String
    newDisc.medDisciplina = item["MEDIA_DISCIPLINA"]! as? String
    newDisc.nomProfessor = item["NOM_PESSOA_PROF"]! as? String
    newDisc.perFaltas = item["PERCENTUAL_AULAS"]! as? String

       //Eu acho que não seja assim que se capture o valor
       for nota in (item["NOTAS"]! as! NSArray){

           //nota has no subscription members     
           print(nota["COD_AVALIACAO"] as String)

       }
        //Adiciono os itens na lista
        self.listDisciplinasWS.append(newDisc)
 }

Any hint?
I appreciate all your help

1 answer

1


Instead of using NSArray try to use the native Swift types by using the NSArray compiler cannot infer that the array contains dictionaries within it.

for item in resultado!{

    let newDisc = DisciplinasML()

    newDisc.codDisciplina = item["COD_DISCIPLINA"]! as? String
    newDisc.desDisciplina = item["DES_DISCIPLINA"]! as? String
    newDisc.desPeriodo = item["DES_PERIODO"]! as? String
    newDisc.medDisciplina = item["MEDIA_DISCIPLINA"]! as? String
    newDisc.nomProfessor = item["NOM_PESSOA_PROF"]! as? String
    newDisc.perFaltas = item["PERCENTUAL_AULAS"]! as? String

    // Modificação ocorre nessa parte do código
    for nota in (item["NOTAS"]! as! [[String:Any]]){

       //nota has no subscription members     
       print(nota["COD_AVALIACAO"] as! String)

    }

    self.listDisciplinasWS.append(newDisc)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.