I’m reading the documentation on JsonUtility.FromJson and find it hard to believe, that I can’t serialize or deserialize an array of serializable objects. This works fine with plugins like LitJson, I was using before, so now I thought it was great, that Unity has json support built-in. Am I doing something wrong or is this maybe a feature to come in the future?
// Create some already tested dummy data.
User user = new User();
user.DisplayName = "TestName";
// Works fine with single object
string json = JsonUtility.ToJson(user);
Debug.Log(json); // returns valid json string.
Debug.Log(JsonUtility.FromJson<User>(json)); // works as it should.
// Doesn't work in array.
User[] usersArray = new User[3] {user,user,user};
string json2 = JsonUtility.ToJson(usersArray);
Debug.Log(json2); // returns "{}"
Debug.Log(JsonUtility.FromJson<User>(json2)); // is empty.
Thanks!
Serializing/deserializing arrays and lists as top-level elements is not supported right now. It’s on the to-do list…
Note that you can work around it in two ways for now:
-
Use ToJson() for each element in the array separately, and just stitch them together with “[”, “,” and “]”
-
Wrap the array in a structure like this:
[Serializable]
public struct MyObjectArrayWrapper
{
public MyObject objects;
}
Here is my implementation of a JsonUtility wrapper for arrays and lists and also nested arrays and lists.
void ParseJsonToObject(string json)
{
var wrappedjsonArray = JsonUtility.FromJson<MyWrapper>(json);
}
[Serializable]
private class MyWrapper
{
public List<MyObject> objects;
}
[Serializable]
private class MyObject
{
public int variable1;
public string variable2;
public List<MyNestedObject> nestedObjects;
}
[Serializable]
private class MyNestedObject
{
public string nestedVariable1;
public string nestedVariable2;
}
And the json would look like this
{ "objects": [
{
"variable1": "abc123",
"variable2": "def456",
"variable3": [
{
"nestedVariable1": "ghi789",
"nestedVariable2": "jkl101"
},
{
"nestedVariable1": "zxy789",
"nestedVariable2": "wfg101"
}
]
},
{
"variable1": "3qaghh5",
"variable2": "34qhah",
"variable3": [
{
"nestedVariable1": "3qwe54hb",
"nestedVariable2": "4ahb4e"
},
{
"nestedVariable1": "gverazh4",
"nestedVariable2": "233ghs"
}
]
}
] }
a simple wrapper to support array
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;
}
}
I just tested it in Unity 5.3.5 declaring the field like this:
public Obbj obbs = new Obbj { new Obbj(), new Obbj() };
And it works
It seems they added the array support
It is now the future, year 2021, a big pandemic has striken the whole planet.
And seems like we still can’t serialize Arrays as top-level elements without a wrapper.
Has this issue been forgotten completely or I am missing something?
hear hear… i have no problems with single arrays like
public int[] progress;
to json saved file but not a nested one such as
public int[][] completed;
it’s a simple 6x4 table and when i run my json save to file
string saveString = JsonUtility.ToJson(saveObject);
SaveSystem_JSON.Save(saveString);
it’s simply thrown in the garbage bin, the generated .txt file has all the simple int / int / Vector3 / bool etc… except for the nested array.
I can’t believe this is still too much to ask