Greetings!
I'm working on a class that loads and parses .json files and returns information from the loaded .json, but I have problems reading the nested arrays of my .json files.
Turning the serialized object public and looking at the Inspector, the 'Choises' array is not being read at all. It contains four objects, each contain a string and an integer correspondingly.
using SimpleJSON;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class JsonParser : MonoBehaviour
{
private string loadedJson;
public StoryData storyData;
[Serializable]
public struct choice
{
public string choiceText;
public int choicePointer;
}
[Serializable]
public struct storyImage
{
public string imagePath;
}
[Serializable]
public struct storyText
{
public string text;
}
[Serializable]
public struct placeholderStory
{
public string storyText;
public string storyImage;
public List<choice> choises;
}
[Serializable]
public struct StoryData
{
public placeholderStory[] placeholderStory;
}
public void LoadJsonFile(string filename)
{
loadedJson = File.ReadAllText(Application.dataPath + filename);
storyData = JsonUtility.FromJson<StoryData>(loadedJson);
}
public string GetChoiceText(int storyPosition, int choiceIndex)
{
string choiceString = storyData.placeholderStory[storyPosition].choises[choiceIndex].choiceText;
Debug.Log(choiceString);
return choiceString;
}
public string GetStoryText(int storyPosition)
{
string storyString = storyData.placeholderStory[storyPosition].storyText;
return storyString;
}
public string GetStoryImage(int storyPosition)
{
string imageString = storyData.placeholderStory[storyPosition].storyImage;
return imageString;
}
}
