I'm trying to create a custom revision quiz program in Unity C# which allows users to load questions into the program using JSON files, structured as below:
{
"question": [
{
"title": "What wave characteristic is measured on the vertical axis?",
"answers": {
"correct": "Amplitude",
"wrong": [
"Frequency",
"Period",
"Speed"
]
}
},
{
"title": "Which of these is a vector quantity?",
"answers": {
"correct": "Velocity",
"wrong": [
"Speed",
"Time",
"Mass"
]
}
}
]
}
I've managed to get my program reading from a file using a StreamReader, but am having a lot of trouble trying to get it into a single data structure.
I have seen other solutions using classes and manually defining structures for their solutions, but I don't know how to go about implementing this for a) as complex a structure as this and b) a structure that can have an arbritrary number of items in it (I'd like to support any number of questions). If the best way is to define these classes, how do I go about referencing items inside them? In the past I've parsed JSON using Python 3.6's json
library's json.loads()
function, and that worked perfectly, creating a single multidimensional array / dictionary structure that I could work with easily.
To put it simply, I currently have a string that I've read from a file with JSON data in it. How do I synthesise this into a single array that I can easily access using, eg, questions[question][0]["title"]
, which would return "What wave characteristic is measured on the vertical axis?"
in the above case?