Consider the following C# code in Unity that serializes an object to JSON. What will be printed to the console?
using UnityEngine; public class Player { public string name; public int score; } public class Test : MonoBehaviour { void Start() { Player player = new Player { name = "Alice", score = 42 }; string json = JsonUtility.ToJson(player); Debug.Log(json); } }
Look at how JsonUtility serializes public fields exactly as named.
JsonUtility serializes public fields with their exact names and values. It does not add extra nesting or change case.
In Unity, which class provides methods to convert objects to and from JSON format?
Think about Unity's built-in JSON support.
Unity provides the JsonUtility class for JSON serialization and deserialization.
Given this code snippet, why does the deserialization not populate the fields?
using UnityEngine; public class Player { private string name; private int score; } public class Test : MonoBehaviour { void Start() { string json = "{\"name\":\"Bob\",\"score\":100}"; Player player = JsonUtility.FromJson<Player>(json); Debug.Log(player.name + ": " + player.score); } }
Check the access level of the fields being deserialized.
JsonUtility only serializes and deserializes public fields. Private fields are ignored.
Unity's JsonUtility does not directly serialize lists. Which approach correctly serializes a list of Player objects?
Think about how JsonUtility handles complex types like lists.
JsonUtility requires a wrapper class with a public array or list field to serialize collections.
You have JSON data with nested objects like this:
{"player":{"name":"Eve","score":75}}
How do you deserialize it correctly in Unity?
Think about matching the JSON structure with your classes.
JsonUtility requires the class structure to match JSON structure. A wrapper class with a Player field matches the nested JSON.