Question by
ethanlramsay · Sep 22, 2017 at 08:50 PM ·
nullreferenceexceptionjson
JSON Utility always returning NULL
I've been trying to get JSON to parse for the last few days, unsuccessfully. I've validated the JSON, can debug the jsonString to the console but as soon as jsonUtility gets its hand on the json then I end up with a load of NULL data.
Here is my json:
{"question": {"questionType": "0", "categoryID": "0", "bonusBool": "FALSE", "answerText": "cake", "questionText": "If something is really easy to do, it\u2019s a piece of \u2026\u2026\u2026 \\n a. pie \\n b. tart \\n c. cake", "ID": "0"}}
Here is my C#:
[System.Serializable]
public class QuestionDataAsString
{
public string questionText;
public string answerText;
public string categoryID;
public string ID;
public string questionType;
public string bonusBool;
}
[System.Serializable]
public class Question{
public string questionText;
public string answerText;
public int categoryID;
public int ID;
public int questionType;
public bool bonusBool;
}
public static void LoadQuestion()
{
string filePath = Path.Combine(Application.streamingAssetsPath, "questionData.json");
string dataAsJSON = File.ReadAllText(filePath);
Debug.Log(dataAsJSON);
QuestionDataAsString question = JsonUtility.FromJson<QuestionDataAsString>(dataAsJSON);
Debug.Log(question.questionText);
}
public static Question ConvertQuestionDataFromString(QuestionDataAsString stringData)
{
Question question = new Question();
question.questionText = stringData.questionText;
question.answerText = stringData.answerText;
question.categoryID = int.Parse(stringData.categoryID);
question.ID = int.Parse(stringData.ID);
question.questionType = int.Parse(stringData.questionType);
question.bonusBool = Boolean.Parse(stringData.bonusBool);
return question;
}
When I run LoadQuestion through a splashscreen controller, I can log the json to console as a string. But I get Null objects once I use jsonUtility.
This is the console output:
{"question": {"questionType": "0", "categoryID": "0", "bonusBool": "FALSE", "answerText": "cake", "questionText": "If something is really easy to do, it\u2019s a piece of \u2026\u2026\u2026 \\n a. pie \\n b. tart \\n c. cake", "ID": "0"}}
UnityEngine.Debug:Log(Object)
SaveLoadManager:LoadQuestion() (at Assets/Scripts/SaveLoadManager.cs:91)
<Start>c__Iterator0:MoveNext() (at Assets/Scripts/SplashController.cs:18)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
Null
UnityEngine.Debug:Log(Object)
SaveLoadManager:LoadQuestion() (at Assets/Scripts/SaveLoadManager.cs:93)
<Start>c__Iterator0:MoveNext() (at Assets/Scripts/SplashController.cs:18)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
I've searched through the answers here and stack overflow, but have not found any relevant solutions.
Any ideas why fromJson is returning Null for everything?
Comment
Answer by ethanlramsay · Sep 26, 2017 at 03:31 PM
Someone on stack overflow found the issue and laid out a working solution
Your answer
![](https://koobas.hobune.stream/wayback/20220612135330im_/https://answers.unity.com/themes/thub/images/avi.jpg)