Menu
CoddyTech

JsonUtility

Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 24 of 50.

JSON is text that holds an object's values, so it can be logged, sent or written to a file. JsonUtility.ToJson(obj) turns an object into JSON and JsonUtility.FromJson<T>(json) makes a new T from it. JsonUtility follows the rules the Inspector follows, so it works on a [System.Serializable] class:

[System.Serializable]
public class PlayerProfile
{
    public string playerName = "Ana";
    public int level = 7;
    public List<string> badges = new List<string> { "Explorer", "Sharpshooter" };
    [SerializeField] private float hoursPlayed = 12.5f;
    private int secretCode = 1234;
    public int Rank { get; set; } = 3;
}
string json = JsonUtility.ToJson(new PlayerProfile());
// {"playerName":"Ana","level":7,"badges":["Explorer","Sharpshooter"],"hoursPlayed":12.5}
PlayerProfile copy = JsonUtility.FromJson<PlayerProfile>(json);

secretCode and Rank are missing. JsonUtility writes public fields and private fields marked [SerializeField], and nothing else: no properties, no static fields, no plain private fields. A Dictionary field is skipped too, and so is a field whose class is not [System.Serializable]. Those are the fields the Inspector leaves out as well. JsonUtility.ToJson(obj, true) writes the same JSON spread over several lines, easier to read.

A list inside a class is written, as badges is. A list on its own is not: JsonUtility needs an object at the top, so JsonUtility.ToJson(scores) for a List<int> of 10, 20 and 30 returns {}. Put the list in a class and convert that; a ScoreBoard holding the same list is written as {"scores":[10,20,30]}:

[System.Serializable]
public class ScoreBoard
{
    public List<int> scores;
}
challenge icon

Challenge

Medium

The team builder lets players share a team as a code. Space should log the team as JSON, and LoadCode(string code) should read a code back. Right now Space logs Team code: {}, and the heroes' strength and agility are not even in the Inspector.

  • In CharacterBuild, make strength and agility saved values: replace the Strength property with a public field strength, and keep agility private but mark it [SerializeField].
  • A list on its own is written as {}. Below CharacterBuild, write a [System.Serializable] class Team with one public field heroes, a List<CharacterBuild>. In TeamCode, replace the heroes list with a public field team of type Team, and log Team code: followed by the JSON of team.
  • In LoadCode, read the code into team with JsonUtility, and log how many heroes it holds, as Loaded 3 heroes.

The Inspector fills team with a Knight (strength 8, agility 3) and an Archer (strength 4, agility 9). Press Play and press Space, read the code in the Console, then Check.

Try it yourself

using UnityEngine;

[System.Serializable]
public class CharacterBuild
{
    public string hero;
    public int Strength { get; set; }
    private int agility;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Game Architecture in Unity