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"
}
]
}]
}
Connectez-vous ou Inscrivez-vous pour répondre.
Réponses
Je ne comprends pas. Tu veux parser un JSON ou en construire ?
est un tableau de Dictionnaire donc ta condition devrait plus ressembler à ceci :
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
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.
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.
merci
Merci beaucoup