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

How to Serialize and Deserialize JSON and JSON Arrays in Unity using JsonUtility?

Linda Hamilton
Release: 2025-02-03 04:07:10
Original
643 people have browsed it

JSONUTILITY serialization and desertation JSON and JSON arrays are used in Unity.

Question

How to use C#to parse and process JSON data (single object and array) in unity, especially using BoomLagoon.json, minijson or jsonutility?

Answer

Starting from version of Unity 5.3.3, it is recommended to use JSONUTILITY to process JSON data because it has high performance and easy use.

<.> 1. The serialization and derivativeization of a single data object:

serialization:

Output: Capitalization:

Player playerInstance = new Player();
playerInstance.playerId = "8484239823";
playerInstance.playerLoc = "Powai";
playerInstance.playerNick = "Random Nick";
string playerToJson = JsonUtility.ToJson(playerInstance);
Copy after login

<.> 2. The serialization and derivative of the JSON array:
{"playerId":"8484239823","playerLoc":"Powai","playerNick":"Random Nick"}
Copy after login

In order to handle the JSON array, you can use the auxiliary class from this github warehouse:

<辅> Auxiliary class -jsonhelper.cs
string jsonString = "{\"playerId\":\"8484239823\",\"playerLoc\":\"Powai\",\"playerNick\":\"Random Nick\"}";
Player player = JsonUtility.FromJson<Player>(jsonString);
Copy after login

<序> serialization:

Output: <<> <序> Capitalization:

public static class JsonHelper
{
    public static T[] FromJson<T>(string json)
    {
        Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
        return wrapper.Items;
    }

    public static string ToJson<T>(T[] array)
    {
        Wrapper<T> wrapper = new Wrapper<T>();
        wrapper.Items = array;
        return JsonUtility.ToJson(wrapper);
    }

    private class Wrapper<T>
    {
        public T[] Items;
    }
}
Copy after login

Other precautions <其他>

<以> For JSON with digital or digital attributes:
Player[] players = new Player[2];
players[0] = new Player { playerId = "8484239823", playerLoc = "Powai", playerNick = "Random Nick" };
players[1] = new Player { playerId = "512343283", playerLoc = "User2", playerNick = "Rand Nick 2" };
string playersToJson = JsonHelper.ToJson(players);
Copy after login
You can consider using Simplejson in Unity Wiki.

Jsonutility failure:
{"Items":[{"playerId":"8484239823","playerLoc":"Powai","playerNick":"Random Nick"},{"playerId":"512343283","playerLoc":"User2","playerNick":"Rand Nick 2"}]}
Copy after login
ensure that the class is not an array, has

attributes, and members are not defined as attributes (delete ).

string jsonString = "{\"Items\":[{\"playerId\":\"8484239823\",\"playerLoc\":\"Powai\",\"playerNick\":\"Random Nick\"},{\"playerId\":\"512343283\",\"playerLoc\":\"User2\",\"playerNick\":\"Rand Nick 2\"}]}";
Player[] players = JsonHelper.FromJson<Player>(jsonString);
Copy after login

This Revised Output Maintains The Original Image and ITS Format, Rephrases The Text for Improved Flow and Clarity, and users consistent code formting. The core information remains unchanged.

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

    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