0
0
Unityframework~20 mins

JSON serialization in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unity JSON Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Unity JSON serialization code?

Consider the following C# code in Unity that serializes an object to JSON. What will be printed to the console?

Unity
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);
    }
}
A{"name":"Alice","score":42}
B{"Name":"Alice","Score":42}
C{"name":Alice,"score":42}
D{"player":{"name":"Alice","score":42}}
Attempts:
2 left
💡 Hint

Look at how JsonUtility serializes public fields exactly as named.

🧠 Conceptual
intermediate
1:00remaining
Which Unity class is used for JSON serialization?

In Unity, which class provides methods to convert objects to and from JSON format?

AJsonParser
BJsonConvert
CJsonSerializer
DJsonUtility
Attempts:
2 left
💡 Hint

Think about Unity's built-in JSON support.

🔧 Debug
advanced
2:30remaining
Why does this Unity JSON deserialization code fail?

Given this code snippet, why does the deserialization not populate the fields?

Unity
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);
    }
}
AThe JSON string is malformed and causes an error.
BFields are private, so JsonUtility cannot access them.
CJsonUtility requires a constructor with parameters.
DThe Player class must inherit from MonoBehaviour.
Attempts:
2 left
💡 Hint

Check the access level of the fields being deserialized.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly serializes a list of objects to JSON in Unity?

Unity's JsonUtility does not directly serialize lists. Which approach correctly serializes a list of Player objects?

AConvert the list to an array and serialize the array directly.
BSerialize the list directly using JsonUtility.ToJson(playersList).
CWrap the list in a container class and serialize the container.
DUse JsonUtility.SerializeList(playersList) method.
Attempts:
2 left
💡 Hint

Think about how JsonUtility handles complex types like lists.

🚀 Application
expert
3:00remaining
How to deserialize JSON with nested objects in Unity?

You have JSON data with nested objects like this:
{"player":{"name":"Eve","score":75}}
How do you deserialize it correctly in Unity?

ACreate a wrapper class with a public Player field and deserialize into it.
BDeserialize directly into Player class ignoring the wrapper.
CUse JsonUtility.FromJsonOverwrite to update an existing Player instance.
DModify the JSON string to remove the nested object before deserializing.
Attempts:
2 left
💡 Hint

Think about matching the JSON structure with your classes.