Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


FromJson

Create an object from its JSON representation.
Read time 4 minutesLast updated 4 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.JSONSerializeModule

FromJson<T>(string)

Create an object from its JSON representation.
public static T FromJson<T>(string json)

Parameters

json

The JSON representation of the object.

Returns

Type

Description

TAn instance of the object.

Remarks

Internally, this method uses the Unity serializer. The object you're creating and all its fields must meet the requirements for serialization by the Unity serializer. For the full list of these requirements, refer to Serialization rules in the manual.
FromJson
only supports plain classes and structures. It does not support classes derived from
UnityEngine.Object
, such as MonoBehaviour or ScriptableObject. To deserialize data into classes derived from MonoBehaviour or ScriptableObject, use JsonUtility.FromJsonOverwrite instead.
During deserialization,
FromJson
calls the parameterless (default) constructor of the target type if one exists. Calling the constructor also runs field initializers. After the object is created,
FromJson
overwrites values of fields that appear in the JSON with the corresponding JSON values. Fields not present in the JSON keep the values assigned by the constructor or field initializers.
Important: If the type has no parameterless constructor,
FromJson
does not call any constructor, and field initializers do not run. All fields start at their C# type defaults (
0
,
null
,
false
, and so on), and
FromJson
then sets fields that appear in the JSON to the corresponding JSON values. Non-serialized fields that depend on initializers keep their C# type default values instead of receiving the initializer values. To make sure field initializers run during deserialization, add a parameterless constructor.
If the input is null or empty,
FromJson
returns null.
FromJson
can be called from background threads.
The following example illustrates what happens when a type has only a parameterized constructor. Because
EnemyInfo
has no parameterless constructor,
FromJson
does not call any constructor, and field initializers do not run. The
[NonSerialized]
field keeps its default value (
0
) rather than the value the initializer would have assigned to it.

Examples

using UnityEngine;public class FromJsonTest : MonoBehaviour{ public static string completeJson = "{\"name\":\"Dr Charles\",\"lives\":3,\"health\":0.8}"; // Partial JSON, missing lives and health. In this example, these fields will get their values from the initializer and constructor respectively. public static string partialJson = "{\"name\":\"Dr Charles\"}"; void Start() { PlayerInfo player1 = PlayerInfo.CreateFromJSON(completeJson); Debug.Log("Player1 Name: " + player1.name); // Dr Charles Debug.Log("Player1 Lives: " + player1.lives); // 3 Debug.Log("Player1 Health: " + player1.health); // 0.8 PlayerInfo player2 = PlayerInfo.CreateFromJSON(partialJson); Debug.Log("Player2 Name: " + player2.name); // Dr Charles (from JSON) Debug.Log("Player2 Lives: " + player2.lives); // 2 (from initializer) Debug.Log("Player2 Health: " + player2.health); // 1 (from constructor) }}[System.Serializable]public class PlayerInfo{ public string name = "Unknown"; public int lives = 2; public float health; public PlayerInfo() { health = 1.0f; } public static PlayerInfo CreateFromJSON(string jsonString) { return JsonUtility.FromJson<PlayerInfo>(jsonString); }}
using UnityEngine;using System;public class EnemyInfoTest : MonoBehaviour{ void Start() { EnemyInfo enemy = JsonUtility.FromJson<EnemyInfo>("{\"serializedField\":42}"); Debug.Log("serializedField: " + enemy.serializedField); // 42 (from JSON) Debug.Log("nonSerializedField: " + enemy.nonSerializedField); // 0 (NOT 100, initializer did not run) }}[Serializable]public class EnemyInfo{ public int serializedField; [NonSerialized] public int nonSerializedField = 100; // Only a parameterized constructor: no parameterless constructor exists. // As a result, FromJson skips constructor invocation entirely // and the field initializer on nonSerializedField never runs. public EnemyInfo(int value) { serializedField = value; } // To make the field initializer run during deserialization, add a // parameterless constructor, for example: // public EnemyInfo() { }}

FromJson(string, Type)

Create an object from its JSON representation.
public static object FromJson(string json, Type type)

Parameters

json

The JSON representation of the object.

type

The type of object represented by the Json.

Returns

Type

Description

ObjectAn instance of the object.

Remarks

Internally, this method uses the Unity serializer. The object you're creating and all its fields must meet the requirements for serialization by the Unity serializer. For the full list of these requirements, refer to Serialization rules in the manual.
FromJson
only supports plain classes and structures. It does not support classes derived from
UnityEngine.Object
, such as MonoBehaviour or ScriptableObject. To deserialize data into classes derived from MonoBehaviour or ScriptableObject, use JsonUtility.FromJsonOverwrite instead.
During deserialization,
FromJson
calls the parameterless (default) constructor of the target type if one exists. Calling the constructor also runs field initializers. After the object is created,
FromJson
overwrites values of fields that appear in the JSON with the corresponding JSON values. Fields not present in the JSON keep the values assigned by the constructor or field initializers.
Important: If the type has no parameterless constructor,
FromJson
does not call any constructor, and field initializers do not run. All fields start at their C# type defaults (
0
,
null
,
false
, and so on), and
FromJson
then sets fields that appear in the JSON to the corresponding JSON values. Non-serialized fields that depend on initializers keep their C# type default values instead of receiving the initializer values. To make sure field initializers run during deserialization, add a parameterless constructor.
FromJson
can be called from background threads.