Hello All, I’m trying to figure out a way to have my game parse a json file for story elements, I created the classes needed, but I can’t for the life of me figure out a proper solution the code parsing out the full json file and making it traversable.
[System.Serializable]
public class Responses
{
public List<string> Resps { get; set; }
public int SubchapForTop { get; set; }
public int SubchapForBottom { get; set; }
}
[System.Serializable]
public class Root
{
public bool ChapComplete { get; set; }
public SubChap[] SubChaps { get; set; }
}
[System.Serializable]
public class SubChap
{
public List<string> TextList { get; set; }
public List<string> ImageList { get; set; }
public List<object> SubList { get; set; }
public List<object> DomList { get; set; }
public List<double> ResponseTime { get; set; }
public Responses Responses { get; set; }
}
public TextAsset jsonFile;
public SubChap subchaps = new SubChap();
void Start()
{
subchaps = JsonUtility.FromJson<SubChap>(jsonFile.text);
Debug.Log(subchaps);
}
This is what I have. Ideally, I’d like it to parse the whole file, and then I can traverse downwards from each subchap to responses based on which choice the player makes then later also check the root file for chapter completion so I can load another chapter. Can anyone help me?
second, Create an instance of your object outside of the Start function so that you can see it in the viewer in unity when it parses (This eliminates the need to Debug.Log crap to see what is and isn’t being parsed correctly) E.G.
public Chapter myChapter = new Chapter();
next put this inside of start to actually have it read the file:
And now to the thing that was breaking my god damn code and wasted probably 4 hours of my life today.
When setting the schemas for your classes, even json2csharp will have this next to each variable:
{ get; set; }
This for some reason I can’t explain because I don’t know enough about unity or c# makes the damn json file not parse correctly and only by messing around and going for broke did I figure out that to fix this issue you need to change your schemas from
public class Responses
{
public List<string> Resps { get; set; }
public int SubchapForTop { get; set; }
public int SubchapForBottom { get; set; }
}
public class Root
{
public bool ChapComplete { get; set; }
public List<SubChap> SubChaps { get; set; }
}
public class SubChap
{
public List<string> TextList { get; set; }
public List<string> ImageList { get; set; }
public List<object> SubList { get; set; }
public List<object> DomList { get; set; }
public List<double> ResponseTime { get; set; }
public Responses Responses { get; set; }
}
to:
public class Responses
{
public List<string> Resps;
public int SubchapForTop;
public int SubchapForBottom;
}
public class Root
{
public bool ChapComplete;
public List<SubChap> SubChaps;
}
public class SubChap
{
public List<string> TextList;
public List<string> ImageList;
public List<object> SubList;
public List<object> DomList;
public List<double> ResponseTime;
public Responses Responses;
}
Once you’ve made these changes run your code and everything will be read from it perfectly, have fun.