I’m trying to create a multiple choice trivia game which selects random questions from an offline database (think Quizoid) and displays it on a panel/image. Also I’m absolutely new to Unity and most of the tutorials and forums go right over my head. Here is a sample from the .json file:
{
"quiz":[
{
"index":"1",
"category":"Geography",
"question":"What is the capital of Tunisia?",
"answer":[{
"1":"Tunis",
"2":"Ulan Bator",
"3":"Prague",
"4":"Limpopo"
}],
},
{
"index":"2",
"category":"Science",
"question":"Which chemical element is represented by Dy?",
"answer":[{
"1":"Dysprosium",
"2":"Darmstadtium",
"3":"Dubnium",
"4":"Dungsten"
}],
},
{
"index":"3",
"category":"History",
"question":"Which animal was famously utilized by Hannibal in his battles?",
"answer":[{
"1":"Elephants",
"2":"Tigers",
"3":"Wolves",
"4":"Eagles"
}],
},
]
}
I’ve tried this:`
void Start()
{
jsonString = File.ReadAllText(Application.dataPath + "/StreamingAssets/questions.json");
itemData = JsonMapper.ToObject(jsonString);
Debug.Log(itemData["quiz"][34]["question"]);
}
but it usually gives this error : “KeyNotFoundException: The given key was not present in the dictionary.”
I’ve tried : - YouTube
How to Read JSON Data in Unity (LitJSON) - YouTube
Is it that i got the coding wrong or should I just use sqlite/xml? Any help is greatly appreciated