- Home /
JsonUtility : When I'm saving/loading to/from .Json, do I need a C# object for every child object of my .json file?
Hey guys! I'm working on creating a flexible Unit Editor scene within unity to allow me to more easily mass-produce different kinds of units for my game. I want to store the data of each unit as multi-layered/nested JSON objects. For instance, every Unitdata JSON object has within it ParameterData, AnimationData, and CodexData which then each have child parameters, etc.
Now, I've followed a couple of tutorials but none of them are dealing with a situation quite as complex as mine.
Each of them are simply pulling from single JSON objects that have a bunch of properties, and in their scripts/code they have a matching Class that represents the entire JSON object and all of its properties. So my question is this: do I really have to make a class(object) for every single sub-object within my JSON?
Like do I need to make a ParameterData class and a CodexData class, etc? and then my UnitData class simply has properties made of all of its child classes?
And then... when I do JsonUtility.FromJson/ToJson, if I pass in the most parent object, will it be able to automatically get all of the data within all of those child objects? Like will I need to loop/enumerate through all the objects somehow, or anything like that?
Here is my .json file I'm working with, if it helps:
Answer by Bunny83 · Sep 13, 2020 at 01:18 AM
When you use Unity's JsonUtility, yes, you have to create a class for every object in your json structure since the JsonUtility is an object mapper / serializer. However instead of Unity's JsonUtility you could also use my SimpleJSON framework. It just parses the json text into a custom object tree and you can access any element node by string / index. For example
// parse your json text
var node = JSON.Parse(yourJsonText);
string unitName = node["unitData"]["parameterData"]["unitName"].Value;
// get a sub node in a local variable for later use
JSONNode storyData = node["unitData"]["storyData"];
int age = storyData["age"].AsInt;
float width = storyData["age"].AsFloat;
Note that I created an extension file which adds extra support for some of Unity's primitive types. Specifically Vector2/3/4, Quaternion, Rect, ... which makes it easier to read and write such values. This extension file just need to sit next to the SimpleJSON.cs file.
So in your example, var node = JSON.Parse(yourJsonText);
is "yourJsonText" a string of the entire JSON file's contents? And, just so I understand you a bit more clearly, did you mean that I would NOT have to make a class inheritance structure for the entire JSON object if I used your tool? As in I could pass it my complete JSON file and your tool could parse through the whole thing as is without needing lots of complex class structure?
Yes, exactly. yourJsonText is any valid json text which is parsed into a node tree which you can directly query. A JSONObject is represented internally by a Dictionary<string, JSONNode>
and an array is represented by a List<JSONNode>
- For the actual value types there are seperate strongly typed classes like JSONNumber (which is a double value), JSONBool (which is just a bool value), JSONNull (to represent null values) and JSONString (to represent a string value).
The JSONNode base class has several conversion properties and indexers to make working with the structure as simple as possible. The example code I've posted should work with your json text as it is written.