-1

So I have two c# classes (Team and Player) and I want to store a team into a json file. Every team has some players. I think it should look like this:

{
    "name":"Chelsea"
    "players": [
        {
            "name":"Player1"
        },
        {
            "name":"Player2"
        },
        {
            "name":"Player3"
        }
    ]
}

But sadly it isn't working for me. It always only returns:

{"name":"Chelsea"}

I do not really know why this happens. Is it even possible to do this? My code is:

public class Team
{
    public string name = "testTeam";
    public Player[] players = new Player[0];

    public void AddPlayer(Player player)
    {
        int i = players.Length;
        players = new Player[i+1];
        players[i] = player;
    }

    public void save()
    {
        File.WriteAllText(Application.dataPath + "/data/teams/" + name + ".json", JsonUtility.ToJson(this));
    }
}

public class Player
{
    public string name;
}
8
  • stackoverflow.com/questions/29808718/… Commented Jan 20, 2021 at 14:50
  • thx I will take a look at this! Commented Jan 20, 2021 at 14:52
  • Check out the Unity docs. It especially has a note regarding arrays. Commented Jan 20, 2021 at 14:52
  • @RomanRyzhiy This post is not really about what my problem is. I can't even save an array in json, in the post the array is just saved in a wrong way Commented Jan 20, 2021 at 14:54
  • @ErikT. You're right so I can't use this method, but I do not know an alternative to this ... any ideas? Commented Jan 20, 2021 at 14:56

2 Answers 2

1

See Serialization Rules

How to ensure a custom class can be serialized

Ensure it:

  • Has the [Serializable] attribute
  • Is not abstract
  • Is not static
  • Is not generic, though it may inherit from a generic class

To ensure the fields of a custom class or struct are serialized, see How to ensure a field in a script is serialized, above.

You should add the Attribute [Serializable]

[Serializable]
public class Team
{
    ...
}

[Serializable]
public class Player
{
    ...
}

A very good hint which things will be correctly serialized by Unity's built-in JsonUtility gives the Inspector since it uses the same serialization rules.

Just put somewhere a

public class Example : MonoBehaviour
{
    public Team team;
}

and you will see if it is correctly serialized or not in the Inspector in Unity.


Then also see Jawad's answer for an optimized way for dynamically storing entries ;)


As a fallback for a more powerful JSON library with less restrictions you can always switch to the Newtonsoft Json.NET Unity package

Sign up to request clarification or add additional context in comments.

Comments

1

One of the problems I see with your code is,

public Player[] players = new Player[0];

public void AddPlayer(Player player)
{
    int i = players.Length;
    players = new Player[i+1];
    players[i] = player;
}

Each time you add a player, you throw away the old players and only add new player to the end of the array.

Instead of array, I believe List would suite you best in this case,

public List<Player> players = new List<Player>();

public void AddPlayer(Player player)
{
    players.Add(player);
}

For serialization, (as per @derHugo 's recommendation), add the [Serializable] Attribute to your class.

[Serializable]
public class Player
{
    public string name;
}
...

1 Comment

Oh I am stupid... thanks I didn't think enough xD

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.