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
The JSON representation of the object.
Returns
Type | Description |
|---|---|
| T | An 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.
FromJsonUnityEngine.ObjectDuring deserialization, calls the parameterless (default) constructor of the target type if one exists. Calling the constructor also runs field initializers. After the object is created, 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.
FromJsonFromJsonImportant: If the type has no parameterless constructor, does not call any constructor, and field initializers do not run. All fields start at their C# type defaults (, , , and so on), and 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.
FromJson0nullfalseFromJsonIf the input is null or empty, returns null.
FromJsonFromJsonThe following example illustrates what happens when a type has only a parameterized constructor. Because has no parameterless constructor, does not call any constructor, and field initializers do not run. The field keeps its default value () rather than the value the initializer would have assigned to it.
EnemyInfoFromJson[NonSerialized]0Examples
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
Returns
Type | Description |
|---|---|
| Object | An 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.
FromJsonUnityEngine.ObjectDuring deserialization, calls the parameterless (default) constructor of the target type if one exists. Calling the constructor also runs field initializers. After the object is created, 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.
FromJsonFromJsonImportant: If the type has no parameterless constructor, does not call any constructor, and field initializers do not run. All fields start at their C# type defaults (, , , and so on), and 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.
FromJson0nullfalseFromJsonFromJson