Home > Backend Development > C++ > How to Serialize and Deserialize JSON and JSON Arrays in Unity using C#?

How to Serialize and Deserialize JSON and JSON Arrays in Unity using C#?

Susan Sarandon
Release: 2025-02-03 04:26:13
Original
226 people have browsed it

How to Serialize and Deserialize JSON and JSON Arrays in Unity using C#?

Use C#serialization and desertileization of JSON and JSON arrays in unity

Question:

You want to use C#serialization and derivativeized JSON data (including the JSON array) in Unity. The BoomLagoon.json library only retrieves the first object when the back -sequentialization list.

Solution:

Unity added JSONUTILITY to API after version 5.3.3. This API allows lightweight serialization and derivativeization of the data type. The serialization of a single data object (non -array of JSON) (non -array of JSON) and the derivativeization

serialization:

Use to convert the object to JSON.

For the format output, use .

  • Capitalization: JsonUtility.ToJson(object obj);
  • JsonUtility.ToJson(object obj, bool prettyPrint); uses to turn JSON back order into a specific type T.
You can also use Calcidity to be turned into a specified type.

To cover the existing examples, use .

  • Sequence and desertileization of multiple data objects (array JSON) JsonUtility.FromJson<T>(string json);
  • To serialize and deactivate the JSON array, you need a auxiliary class, such as JSONHELPER (provided in the code example below). JsonUtility.FromJson(string json, Type type);
  • serialization:
  • JsonUtility.FromJsonOverwrite(string json, object objectToOverwrite);
Create an object array and use

to convert it to JSON. For the format output, use

.

Capitalization:

    Use to serialize JSON back to an array.
  • JsonHelper.ToJson(T[] array)
  • Additional description:
  • JsonHelper.ToJson(T[] array, bool prettyPrint)
Unity's JSONUTILITY itself does not support the array.

To be serialized or retired, JSON starts with digital or digital attributes, consider using the Simplejson library.

Delete
    to ensure compatibility with JSONUTILITY.
  • JsonHelper.FromJson<T>(string json)
  • Code example:

Serialized single data object:

  • A single data object:
  • Serialized JSON array: get; set;

Capitalized JSON array:

(Please note that the implementation of the class in the code example requires it to supplement it by itself. The class should include

and
<code class="language-csharp">Player playerInstance = new Player();
playerInstance.playerId = "8484239823";
playerInstance.playerLoc = "Powai";
playerInstance.playerNick = "Random Nick";

// 转换为JSON
string playerToJson = JsonUtility.ToJson(playerInstance);
Debug.Log(playerToJson);</code>
Copy after login
methods to process array serialization and desertation. )

The above is the detailed content of How to Serialize and Deserialize JSON and JSON Arrays in Unity using C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template