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 .
JsonUtility.ToJson(object obj);
JsonUtility.ToJson(object obj, bool prettyPrint);
uses To cover the existing examples, use .
JsonUtility.FromJson<T>(string json);
JsonUtility.FromJson(string json, Type type);
JsonUtility.FromJsonOverwrite(string json, object objectToOverwrite);
to convert it to JSON. For the format output, use
.
Capitalization:
JsonHelper.ToJson(T[] array)
JsonHelper.ToJson(T[] array, bool prettyPrint)
To be serialized or retired, JSON starts with digital or digital attributes, consider using the Simplejson library.
DeleteJsonHelper.FromJson<T>(string json)
Serialized single data object:
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>
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!