JSON weird deserialization

Hi. I know this subject has been asked a billion times, but I searched and tried everything I saw and can’t seem to get it done. Maybe help on my specific case could enlighten me.

I have this json file (created outside c# and much longer)

{"0":{"game":[0,0,0,0,0,0,0,0,0],"result":[1,0,0,0]},"1":{"game":[0,0,0,0,0,0,0,0,1],"result":[1,0,0,0]},"2":{"game":[0,0,0,0,0,0,0,0,2],"result":[0,0,0,1]},"3":{"game":[0,0,0,0,0,0,0,1,0],"result":[1,0,0,0]},"4":{"game":[0,0,0,0,0,0,0,1,1],"result":[0,0,0,1]},"5":{"game":[0,0,0,0,0,0,0,1,2],"result":[1,0,0,0]},"6":{"game":[0,0,0,0,0,0,0,2,0],"result":[0,0,0,1]}}

This is somewhat of an array with arrays inside (with keys of course).

I have these classes to help me deserialize it (which to me seem to be the type of data I’m trying to get).

public class who_cares : MonoBehaviour {
    void Start () {
        // Not pertinent stuff before
        Tics results = JsonUtility.FromJson<Tics>(theJson);
    }
}

[System.Serializable]
public class Tics {
	public Games[] values;
}

[System.Serializable]
public class Games {
	public float[] game;
	public float[] result;
}

Somehow, it doesn’t implode nor stop execution, but just returns null. Im completely lost and have been trying and searching for hours. Thank you for anyone who dares help. :slight_smile:

Hi @Guy_Yome

You didn’t mention, what JSON tool you are using. So I take a guess, that you are using built-in JsonUtility.

Your json data (if you go and test it with some online Json tool) isn’t like your class structure; seems like you have an object, that contains several objects, not a list:

{
	"0": {
		"game": [0, 0, 0, 0, 0, 0, 0, 0, 0],
		"result": [1, 0, 0, 0]
	},
	"1": {
		"game": [0, 0, 0, 0, 0, 0, 0, 0, 1],
		"result": [1, 0, 0, 0]
	},

/...

So your input does not match the “pattern” of classes, arrays and fields what JsonUtility expects… and you get a empty result.

So if your classes are like this (slightly modded for my testing):

public class JsonTest : MonoBehaviour
{
 	public TextAsset json;
	public Tics results;

    void Start()
	{
		results = JsonUtility.FromJson<Tics>(json.text);
	}
}

[System.Serializable]
public class Tics
{
	public Games[] values;
}

[System.Serializable]
public class Games
{
	public float[] game;
	public float[] result;
}

And if the data was something like this (read the JsonUtility docs - it explains the limitations):

{
    "values":
    [
        {"game":[1,0,0,0,0,0,0,0,0],"result":[1,0,0,0]},
        {"game":[2,0,0,0,0,0,0,0,0],"result":[2,0,0,0]},
        {"game":[3,0,0,0,0,0,0,0,0],"result":[3,0,0,0]}
    ]
}

…It should read into Unity like this:

123975-20180905-json-test.png