Read Json file for quiz

Hello,
I would like to created a Quiz with this exemple file
How can I do to read it in Flutter please?
And maybe do you have an other solution file
Thank you again for your help

And maybe this exemple json file is not good for you please then tell me how can I do

{
question: ‘AAAAAAAAAAA’,
backgroundColor: ‘Colors.red’,
imageURL: ‘http://www.website.com/img.png’,
videoURL : Null,
answer: ‘aaaa’, true,
answer: ‘bbbb’, false,
answer: ‘cccc’, true,
answer: ‘dddd’, false,
answer: ‘eeee’, false,
answer: ‘ffff’, false,
answer: ‘gggg’, true,
}

{
question: ‘BBBBBBBBBBBBBB’,
backgroundColor: Null,
imageURL: Null,
videoURL : ‘https://www.youtube.com/watch?v=abcdef’,
answer: ‘zzzz’, true,
answer: ‘wwww’, false,
answer: ‘ffff’, false,
}

Hello Carl,

to learn about reading/parsing a JSON in Flutter please check the following tutorial:
https://codingwithjoe.com/dart-fundamentals-working-with-json-in-dart-and-flutter/

Regarding your JSON: the answer properties make it a non valid json structure, you are listing two values for each answer property. I’d use something like the following:

[
  {
    "question": "AAAAAAAAAAA",
    "backgroundColor":  "red",
    "imageURL": "http://www.website.com/img.png",
    "videoURL" : null,
    "answers": ["aaaa", "bbbb", "cccc", "dddd", "eeee", "ffff", "gggg"],
    "solution": "aaaa"
  }, 
  {
    "question": "BBBBBBBBBB",
    "backgroundColor":  "blue",
    "imageURL": "http://www.website.com/img.png",
    "videoURL" : null,
    "answers": ["zzzz", "xxxx", "yyyy"],
    "solution": "xxxx"
  }, ...
]

You will need a Question class to create question objects by parsing the json array.
Answers will be a list of string that you can iterate to show the choices, then you can check the given solution against the value contained in “solution”.
For the bgcolor you will have to switch between the given string values and the actual Flutter Colors constants.

Anyway if you are not getting the json from a remote web-service and your goal is to build the quiz game, and not learning about json in flutter, I don’t see the reason to use json at all. You can implement this data structure in plain Dart, and save you the parsing step. You’d still need a Question class to build your questions list.

Hth

Hello Patrick Thank you for you answer and for your modification
But I would like to have multi answer how can I do? please…
it’s the reason why I did it

answer: ‘aaaa’, true,
answer: ‘bbbb’, false,
answer: ‘cccc’, true,
answer: ‘dddd’, false,
answer: ‘eeee’, false,
answer: ‘ffff’, false,
answer: ‘gggg’, true,

Yes, your intentions were clear from the data structure you proposed, but it is not valid for json, nor you can construct it in Dart either.

Two problems there:
a) answer: 'aaaa', true - This is not valid and you’d have to wrap the two values with an array like: "answer": ["aaaa", true], for each answer.

b) questions have a different number of answers and your structure with multiple “answer” properties will make it tricky to handle that variable number of answers to display. Moreover, if you want to parse the json into a Dart class, you will need the same number of instance variables defined in the class itself, and they cannot have all the same names. So you would have to define them as answer1, answer2… answerN. With N being the max num of answers your questions can have. Again, here you will fight to manage them in an easy way.

For those reasons I suggested that you could list all the possible string answers to be displayed using an array (a list, in Dart), no matter what is their number. Then list all the correct answers in a separate solutions array to be used for response validation: solutions: ["aaaa", "cccc", "gggg"] if your questions can have multiple valid solutions or just ["aaaa"] when there is only one correct solution (so you can have a unique validation function which takes an array, the user response, and checks if it is a member of the given solutions array).

Hth

Thank you Patrick Json is not easy :frowning:

You are welcome, take a little time to learn more about it:

Is it correct for you?
Thank you a lot

[
{
“question”: “aaaaaaaaaa”,
“comment”: “AAAAAAAAAAA”,
“backgroundColor”: “red”,
“imageURL”: “http://www.website.com/img.png”,
“videoURL” : null,
“answers”: [
{ “answer”: [“aaaa”, true ] },
{ “answer”: [“bbbb”, false] },
{ “answer”: [“cccc”, true ] },
{ “answer”: [“dddd”, false] },
{ “answer”: [“eeee”, false] },
{ “answer”: [“ffff”, false] },
},
]

Is syntactically correct, but you don’t need the objects inside the answers array, you can have a simpler array of arrays, you already know they are all answers.