- Home /
How to Integrate a .JSON text array into the program?
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 : https://www.youtube.com/watch?v=bGrTN4670cY https://www.youtube.com/watch?v=OyQQ-7-22Hw
Is it that i got the coding wrong or should I just use sqlite/xml? Any help is greatly appreciated
Answer by aditya · Aug 22, 2016 at 08:00 AM
if you are familiar with List
and Dictionary
then simply go with MiniJSON class ... simplest method i've found on internet even facebook is using this in their SDK to decode their JSON data
Thank you friend. I've solved it though I chose to use LitJson .
Your answer
Follow this Question
Related Questions
string/text database 0 Answers
How to detect an object inside another object? 1 Answer
2D character gets stuck when falling 1 Answer
Multiple Cars not working 1 Answer