How to serialize parameters from JSON?

I’m trying to figure out how I can deserialize some JSON and get it to be usable within Unity. I’m using JSONUtility for the JSON Serialization.


 {"id":1, "nodes":[{"code":101,"indent":0,"parameters":["FacePic1",0,0,2]},{"code":401,"indent":0,"parameters":["This is a message test"]},{"code":0,"indent":0,"parameters":[]}],"name":"","switchId":1,"trigger":0},

I know I need to create a class for the object (Which will be my Event class) and then a ‘nodes’ object to hold the data in ‘nodes’. I’ve already done this for another class… however, how would I tackle the ‘parameters’ of the nodes object? This varies depending on the code of ‘nodes’? For example, code 101 has a string and 3 ints, but code 401 only has a string?

JsonUtility can not serialize / deserialize the object structure you have presented here. It’s valid json (without the final comma) but not supported by Unity’s JsonUtility.

So you have to pick an alternative json framework. Quite commonly used is the Newtonsoft Json.Net library. It can be used to deserialize your parameters array into an object array. Though that means you have to manually check for the types and cast to the right types.

An alternative may be my own SimpleJSON framework. It’s not an object mapper like JsonUtility or the Json.Net library. Instead it just parses the json representation into a set of custom classes to represent the various json data types. There are implicit type convertion operators which allow you to essentially directly use a JSONNode as the actual primitive type you want.

So assuming the trailing comma does not exist, you can parse your json just like this:

JSONNode root = JSON.Parse(yourJsonText);
int id = root["id"];
string name = root["name"];
int switchId = root["switchId"];
int trigger = root["trigger"];
foreachJSONNode node in (root["nodes"])
{
    int code = node["code"];
    int indent = node["indent"];
    foreach(JSONNode param in node["parameters"])
    {
        // do something with each parameter
    }
}

Of course you don’t have to parse out everything. You could keep the parameter array as a JSONNode if you like in case you need them later.

Note that even though SimpleJSON is just a single file, it’s still modular. All classes are declared partial which makes it possible to directly extend almost every aspect with an extention file. There are some extention files in the repository. One that adds support for some common Unity primitive types like vectors, colors, quaternion, … and another one for some additional common .NET types.

Of course under the hood json itself can only represent “numbers” (usually double precision), boolean values (true / false), strings, null and the two different collection types Array and Object. Those are represented by the types JSONNumber, JSONBool, JSONString, JSONNull, JSONArray and JSONObject which are all derived from the JSONNode base class. You can implement your own conversion operators for your own types if you really need them very often.

Of course it depends on your needs and how you’re going to use this data in the end.