- Home /
(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 :)
Answer by AbdelBous · Nov 08, 2016 at 11:20 AM
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 : https://github.com/SaladLab/Json.Net.Unity3D
Thanks, that's actually what someone else recomended as well :) I'm not using the Unity plugin, but I am using the 2.0 version of the newton dll. It's working very well!
Hello , but Json.Net.Unity3d is unable to serialize a GameObject. Is it working for you ?
Answer by matchsun · Mar 20, 2017 at 02:20 PM
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;
}
}
Your answer
Follow this Question
Related Questions
Best Way to Store Large Number of GameObjects? 1 Answer
JsonData to Enum 2 Answers
unity 3d loading data from json problem?! 1 Answer
MiniJSON Multiple Rows 1 Answer