In this post, we’re going to see how to parse the JSON file in Unity 3D C#. JSON is used to handle data in an easy way, you can parse it in Unity 3D to store and display the data in your Game or Application. It stands for Javascript Object Notation, it’s a lightweight and open standard file format for storing and sharing the data. JSON is often used for data transfer from the server to the client. It can be easy to understand, as it uses human-readable text.
If you are new to Unity 3D, here is a brief about Unity 3D. Unity 3D – is one of the game development engines, using Unity 3D we can create 2D and 3D games. Unity 3D is powered by Unity Technologies. It is a cross-platform game engine.
How To Parse | Deserialize the JSON Data in C# – Unity 3D
To parse the JSON file in Unity 3D we need to have this following package. Download Here. This package Author is Matt Schoen -http://www.defectivestudios.com. Once downloaded the package, add it to your project Asset folder.
This JSON Object package will help you to deserialize the JSON files in an easy way. Let us consider a sample JSON data as followed.
Now create a Model in your C# code for the JSON file, I have used
Above is the sample Model class for the JSON file, similarly you have to create your own model class as needed. Make sure that all the model classes mentioned as Serializable.
Read the JSON file and get the JSON string, in Unity 3D there are different data paths are available for file access, they are based on the Operating Systems. Here I’m going to use the StreamingAssets path for this example.
Load JSON file from the system
As mentioned I’m going to access the JSON file from the StreamingAssets folder to make the JSON parsing work on runtime. Then, to load the file, add the following lines in your code and get the JSON string from the file.
The JSON string is successfully read as text data, then we have to parse the JSON string to JSON Object of our Model class. To parse the JSON text, use the following line in your code and store it as JSON Object.
The above line will parse the JSON file as JSON Object. Similarly, you can parse any of the JSON file and access them in your codes. Hope this will be useful to you. Happy Coding!
Comments 1