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. :)
Answer by eses · Sep 04, 2018 at 08:00 PM
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:
Hey! Thanks for answering! So do you think I need to use another Json tool? Im using the basic JsonUtility as you mentionned. Im gonna try to check the structure online too.
I don't get how I don't have the right structure yet. I went to check my structure online. Changed it a bit to simplify (removed the keys).
Looks like
[{"game":[0,0,0,0,0,0,0,0,0],"result":[1,0,0,0]},{"game":[0,0,0,0,0,0,0,0,1],"result":[1,0,0,0]},{"game":[0,0,0,0,0,0,0,0,2],"result":[0,0,0,1]}]
And I got this structure for Json (which is what the tool tells me it is (array of "dictionaries" of two kind of arrays)), but still, no match.
[System.Serializable]
public class Tics {
public Games[] values;
}
[System.Serializable]
public class Games {
public Game game;
public Result result;
}
[System.Serializable]
public class Game {
public int[] game;
}
[System.Serializable]
public class Result {
public int[] result;
}
Your answer
Follow this Question
Related Questions
JsonUtility and Arrays - JSON must represent an object type. 0 Answers
Argument Exception: J SON must represent an object type. Serialization list of objects 0 Answers
Problem with jsonconstructor while deserializing a class 0 Answers
How to reliably send Kinect v2 data over MQTT? 1 Answer
save one big json string vs multi seperate json strings in playerprefs 0 Answers