- Home /
JsonUtility nested array deserialization
Is it possible to deserialize nested arrays from json using JsonUtility.FromJson() from 5.3?
Example:
{
"test": [[0, 1], [2, 3], [4, 5]]
}
Should output into a class with:
int[][] test;
(If I'm not mistaken. This question is about the first example either way.)
from the docs: "Any plain class or structure is supported, as well as classes derived from $$anonymous$$onoBehaviour or ScriptableObject."
what (code) did you try?
I've tried int[][] and int[,] with the above example json, which doesn't seem to work. I've also tried using a seperate class:
public IntArray[] test;
public class IntArray {
public int[] vector2;
}
This DOES work, however only if the json structure is as:
"test": [ { "vector2": [0, 1] }, { "vector2": [2, 3] } ]
I'm trying to store vector2 coordinates and though this version does work, the first json example seems a lot cleaner and readable to me, but I don't know how to get that result.
i just tried the following which didn't even create the expected JSON object.
using UnityEngine;
public class JSONTest : $$anonymous$$onoBehaviour
{
public class JSONTestClass
{
public int[,] TestArray;
public int TestInt;
public string TestString;
}
public void Start()
{
var testClass = new JSONTestClass
{
TestArray = new [,] {{0,0,0,0},{1,1,1,1},{2,2,2,2},{3,3,3,3}},
TestInt = 64,
TestString = "testString"
};
Debug.Log(JsonUtility.ToJson(testClass));
}
}
Answer by superpig · Jan 06, 2016 at 02:39 AM
Unfortunately, the Unity serializer itself does not support the kind of nested array structure you are trying to use, which means that JsonUtility (which is built on the Unity serialiser) does not support it either.
It's possible this will come in a future update but for now the only workaround is to do something like the IntArray setup you already saw. Alternatively, have you tried just using a Vector2[] ?
Hi superpig, do you know if there is any workaround for this? I have JSON that contains nested arrays that is co$$anonymous$$g from another system, so unfortunately don't have the option to changed the structure as suggested above. Thanks!
Yes, simply don't use Unity's JsonUtility. It has the same restrictions as Unity's serialization system. You can use any other Json solution. For example my SimpleJSON parser. It doesn't deserialize to C# classes but to a tree structure of JSONNodes so you can easily access the data. Just look at the examples on the wiki page.
There are other solutions as well, just look around the net.
Thanks @Bunny83. I quite like the idea of deserializing straight into C# classes, though. Is there not a way to workaround/handroll the deserialization of the array of arrays? I came across some approaches using ISerializationCallbackReceiver, though I couldn't figure out if/how that would work for this case.
Answer by atharahmad · Oct 28, 2016 at 01:05 PM
using UnityEngine; using System.Collections.Generic;
public static class JsonParser { public static List FromJsonArray(string json) { List objects = new List (); // start with { ends with }
int start = json.IndexOf ('['); // where array starts
int length;
while ((length = json.IndexOf ("},{", start)) != -1) { // how many objects in json
objects.Add (json.Substring (start + 1, length - start)); // cutting string by finding { and }
start = length + 1;
//Debug.Log (objects [objects.Count - 1]);
}
length = json.IndexOf ("}", start);
objects.Add (json.Substring (start + 1, length - start)); // last object left
//Debug.Log (objects [objects.Count - 1]);
List <T> list = new List <T> ();
foreach (string obj in objects)
list.Add (JsonUtility.FromJson <T> (obj));
return list;
}
} this works fine for this json... [{"id":"wdawdaw","name":"bin","level":"4","subscriptions":"24"},{"id":"wdadsdwda","name":"2daw","level":"2","subscriptions":"3"},{"id":"wdadsdwdasdaw","name":"vas","level":"2","subscriptions":"3"}]
Answer by ElectricMonk · Feb 24, 2017 at 09:00 PM
A good example of how to handle this on Stack Overflow here: http://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity