The task: to deserialize from the json string and put it into a variable. All business happens in Unity

Code:

[Serializable] class packageF { public player[] players = null; } class player { public string name; public float x; public float y; public int Class; //int anim; //bool isRight; } packageF CreateFromJSON(string jsonString) { return JsonUtility.FromJson<packageF> (jsonString); } 

When I use CreateFromJSON (json), it throws an ArgumentException: JSON must represent an object type.

What to do?

  • Please show me an example of json where you get an error - vmchar
  • vmchar, {"players": [{"name": "Oleg", "x": 1, "y": 2, "Class": 0}]} - MrFair61

1 answer 1

Try marking your player class with the [Serializable] attribute, because without the attribute you can only serialize types of familiar units (for example, an int array), but not your custom types.

Also keep in mind that the unit serializer does not support null.

  • Thank you very much! - MrFair61