(SOLVED) Serializing different JSON objects, contained in an array, in another JSON object. (C#)

I receive data in my Unity application, in the form of JSON. The JSON object contains various properties; among them an array of objects. These objects contain different properties based on their type. I have simplified and generalised this example as the data is not public; but the objects in the array (“things”) contain many properties, and the types of object, are very different from one another. Example:

receivedata : {
    "type": "angry stuff",
	"things": [
		{
                    "type": "person",
			"name": "paul",
			"value": 3
		},
		{
                    "type": "person",
			"name": "richard",
			"subname": "this is text"
		},
		{
			"type": "coordinate",
			"x": 0,
			"y": 0,
		}
	]
}

What I’m doing now, is unpacking “receivedata” using JsonUtility.CreateFromJSON, as in various examples around the internet:

[Serializable]
public class JSONReceivedData
{
    public string name;
    public OBJECT? things;

    public static JSONReceivedData CreateFromJSON(string jsonString)
    {
        return JsonUtility.FromJson<JSONUser>(jsonString);
    }
}

What od I replace OBJECT? with? So far I’ve tried creating a json object that only has the “type” property; converting it, and then reconveritng it based on what type it is. First I tried having “things” be a string, or a string array, but both failed to convert to anything. I simple got an empty string, or string array. Then I tried having OBJECT? be a “JSONThing” object, having only a string: type property, which works, but if I then convert that JSONThing object, into fx. a JSONPerson object, I only have the type property; the other properties don’t exist anymore.

So how do I go back and get the data from the original JSON string? The only way I can see is to have several overload versions of the “JSONReceivedData” class, each having the OBJECT? property, be an array of the typed objects (fx. JSONPerson), but that is not feasible, there are a lot of different types of objects, and I need to be able to do this conversion in a reasonable time, not to mention how much code this would amount to. I’ve also tried converting “things” to an object, an object array, a JSONObject and a JSONObject array. Each resulted in no data.

IN summary: How can I go back and ONLY get the “things” property from the original data, or convert it to a generic array, without loosing unique properties? I know this may be hard to follow, and it doesn’t help I have to use a generic example, but I’m really at a loss here. Feel free to ask any questions, and I appreciate you taking the time to read all this :slight_smile:

Hi,

Arrays are not supported correctly with JsonUtility. I recommend you to use newtonsoft json library, it works much better (for array and for ? type too)

Be carefull, don’t use the origanal dll, but the unity-package : GitHub - SaladLab/Json.Net.Unity3D: Forked Newtonsoft.Json to support Unity3D

Hello, maybe you can use the following helper code to do it,

public JsonHelper ()
		{
		}
		//Usage:
		//YouObject[] objects = JsonHelper.getJsonArray<YouObject> (jsonString);
		public static T[] getJsonArray<T>(string json)
		{
			string newJson = "{ \"array\": " + json + "}";
			Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);
			return wrapper.array;
		}
		//Usage:
		//string jsonString = JsonHelper.arrayToJson<YouObject>(objects);
		public static string arrayToJson<T>(T[] array)
		{
			Wrapper<T> wrapper = new Wrapper<T> ();
			wrapper.array = array;
			return JsonUtility.ToJson (wrapper);
		}

		[Serializable]
		private class Wrapper<T>
		{
			public T[] array;
		}
	}