Hello, I’ am trying to reach specific question parameters whenever I want. But I am getting an error even though I made the connection from Unity.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class JSONGReader : MonoBehaviour
{
string jsonString;
public TextAsset jsonTextAsset;
[System.Serializable]
public struct QuestionObject
{
public string categoryType;
public string type;
public string difficulty;
public string question;
public string correctAnswer;
public string[] incorrectAnswers;
}
void Start()
{
ReadData();
}
private void ReadData()
{
jsonString = jsonTextAsset.text;
QuestionObject[] results = JsonHelper.getJsonArray<QuestionObject>(jsonString);
foreach (QuestionObject a in results)
{
Debug.Log(a.categoryType);
}
}
}
public class JsonHelper
{
public static T[] getJsonArray<T>(string json)
{
string newJson = "{ \"array\": " + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
return wrapper.array;
}
[System.Serializable]
private class Wrapper<T>
{
public T[] array;
}
}
{
"response_code": 0,
"results": [
{
"category": "General Knowledge",
"type": "multiple",
"difficulty": "medium",
"question": "What name represents the letter "M" in the NATO phonetic alphabet?",
"correct_answer": "Mike",
"incorrect_answers": [ "Matthew", "Mark", "Max" ]
},
{
"category": "General Knowledge",
"type": "multiple",
"difficulty": "medium",
"question": "Which Italian automobile manufacturer gained majority control of U.S. automobile manufacturer Chrysler in 2011?",
"correct_answer": "Fiat",
"incorrect_answers": [ "Maserati", "Alfa Romeo", "Ferrari" ]
},
{
"category": "General Knowledge",
"type": "multiple",
"difficulty": "medium",
"question": "What is the name of the popular animatronic singing fish prop, singing such hits such as "Don't Worry, Be Happy"?",
"correct_answer": "Big Mouth Billy Bass",
"incorrect_answers": [ "Big Billy Bass", "Singing Fish", "Sardeen" ]
},
{
"category": "General Knowledge",
"type": "multiple",
"difficulty": "medium",
"question": "What is the last letter of the Greek alphabet?",
"correct_answer": "Omega",
"incorrect_answers": [ "Mu", "Epsilon", "Kappa" ]
},
{
"category": "General Knowledge",
"type": "multiple",
"difficulty": "medium",
"question": "Which iconic Disneyland attraction was closed in 2017 to be remodeled as a "Guardians of the Galaxy" themed ride?",
"correct_answer": "Twilight Zone Tower of Terror",
"incorrect_answers": [ "The Haunted Mansion", "Pirates of the Caribbean", "Peter Pan's Flight" ]
},
{
"category": "General Knowledge",
"type": "multiple",
"difficulty": "medium",
"question": "What is real haggis made of?",
"correct_answer": "Sheep's Heart, Liver and Lungs",
"incorrect_answers": [ "Sheep's Heart, Kidneys and Lungs", "Sheep's Liver, Kidneys and Eyes", "Whole Sheep" ]
},
{
"category": "General Knowledge",
"type": "multiple",
"difficulty": "medium",
"question": "Where did the pineapple plant originate?",
"correct_answer": "South America",
"incorrect_answers": [ "Hawaii", "Europe", "Asia" ]
},
{
"category": "General Knowledge",
"type": "multiple",
"difficulty": "medium",
"question": "Directly between the Washington Monument and the Reflecting Pool is a memorial to which war?",
"correct_answer": "World War II",
"incorrect_answers": [ "Vietnam War", "American Civil War", "American Revolutionary War" ]
},
{
"category": "General Knowledge",
"type": "multiple",
"difficulty": "medium",
"question": "The Mexican Beer "Corona" is what type of beer?",
"correct_answer": "Pale Lager",
"incorrect_answers": [ "India Pale Ale", "Pilfsner", "Baltic Porter" ]
},
{
"category": "General Knowledge",
"type": "multiple",
"difficulty": "medium",
"question": "What are the three starter Pokemon available in Pokemon Black and White?",
"correct_answer": "Snivy, Tepig, Oshawott",
"incorrect_answers": [ "Snivy, Fennekin, Froakie", "Chespin, Tepig, Froakie", "Chespin, Fennekin, Oshawott" ]
}
]
}
