Json Quiz Format in Swift 3

Bonjour à  tous,


 


Je voudrais récupérer mes questions d'un fichier JSON que j'associe à  un struct{}

Pour le moment, je récupère sans soucis le contenu du fichier JSON et le struct est aussi fonctionnel (Tester séparément suivant le tuto : https://www.youtube.com/watch?v=dyxqsfrCaeM)

Je voudrais construire mon JSON de façon a pouvoir associé une variable :



//Mark : 1
if let questionsQuiz = json?["questionsQuiz"] as? [[String: AnyObject]] {


for items in questionsQuiz {

//Mark : 2
if let newItem1 = items["poomsae1"] as? [Question] {

var variable = [Question()]

for item in newItem1 {

variable.append(item)

self.Questions = variable

}
//Mark : 3
print("RSJ Questions", Questions)

}else{
//Mark : 4
print("newItem1 type different")


}

}


}

//Mark : 1 = Le format actuel de mon JSON


 


//Mark : 2 = ne se produit jamais puisque le suivant est : if let newItem1 = items["poomsae1"] as? String: AnyObject {.....}


 


//Mark : 3 = NA


 


//Mark : 4 = L'information renvoyé dans la situation actuelle


 


 


 


//JSON



{
"questionsQuiz": [{



"poomsae1": [{

"Question": "Questions Officielles 1 YYYZZZ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{
"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

}
],

"poomsae2": [{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{
"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

}
],
"poomsae3": [{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{
"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

}
],
"poomsae4": [{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{
"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

}
],
"poomsae5": [{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{
"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

}
],
"poomsae6": [{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{
"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

}
],
"poomsae7": [{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{
"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

}
],
"poomsae8": [{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{
"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

},

{

"Question": "Questions Officielles 1 ? ",

"Answers": [

"Reponse 1",
"Reponse 2",
"Reponse 3B",
"Reponse 4"
],
"Answer": 3,

"DetailIfCorrectAnswer": "detailIfCorrectAnswer",

"TypeDeQuestion": "typeDeQuestion"

}
]

}]
}

Réponses

  • Je ne comprends pas. Tu veux parser un JSON ou en construire ?


     



     


     


    poomsae1

     


    est un tableau de Dictionnaire donc ta condition devrait plus ressembler à  ceci :



    if let newItem1 = items["poomsae1"] as? [[String: Any]] { ... }


  •  


    Je ne comprends pas. Tu veux parser un JSON ou en construire ?


     


     


    est un tableau de Dictionnaire donc ta condition devrait plus ressembler à  ceci :



    if let newItem1 = items["poomsae1"] as? [[String: Any]] { ... }



     


    Oui justement j'avais cela aussi.


    Maintenant je veux juste savoir comment constitué mon JSON pour correspondre à  mon système de Quiz construit suivant le tuto

  • trouver


     


    Merci tout le monde :)



    var x = [Question]()

    y = newItem1["question"] as! String

    z = x.append(y)





  • Passe par des conditions quand tu accèdes à  une valeur dans un dictionnaire car tu n'es jamais sur que cette clé existe et qu'elle soit au type auquel tu attends.



    if let question = newItem1["question"] as? String {

    // poursuivre
    // ...
    }
  • GoodGoodGoodGood Membre
    avril 2017 modifié #6

    Bonjour, 


     


    Juste une petite remarque comme sa, Pour parser du JSON je te conseil d'utiliser plutot le "guard let  .. else{return}" que le "if let", ton code sera beaucoup plus court est beaucoup plus lisible. 




    guard let question = newItem1["question"] as? String else {return}


  •  


    Passe par des conditions quand tu accèdes à  une valeur dans un dictionnaire car tu n'es jamais sur que cette clé existe et qu'elle soit au type auquel tu attends.



    if let question = newItem1["question"] as? String {

    // poursuivre
    // ...
    }



    merci :)



  •  


    Bonjour, 


     


    Juste une petite remarque comme sa, Pour parser du JSON je te conseil d'utiliser plutot le "guard let  .. else{return}" que le "if let", ton code sera beaucoup plus court est beaucoup plus lisible. 




    guard let question = newItem1["question"] as? String else {return}



     


    Merci beaucoup 

Connectez-vous ou Inscrivez-vous pour répondre.