JSON is a common data format that stores data in human-readable strings that can be interpreted by computers. This makes it possible for you and other people to read and modify the raw data and pass that information along to internal systems for use. JSON is commonly used across the web and other places and has native support built in to Unity using JSON.NET. In this video we’re going to walk through an introduction into how to serialize and deserialize JSON objects in Unity so you can quickly store configurable information about your games in a standard open format.
JsonConvert.SerializeObject()
: Serializing object’s converts a C# object’s data into a string so you can store and pass that information along to other systems. Passing in a class will return a string of the data serialized into a JSON format. You can read that data back out usingDeserializeObject
.JsonConvert.DeserializeObject()
: Deserializing object’s requires two parameters. The class you want to deserialize the data into and the JSON formatted string you want to deserialize. This method will automatically attempt to convert the provided string into a new instance of the target class and return that class back to you to use. After you’ve modified data you can write the information back to a JSON string withSerializeObject
.
When working with strings that span multiple lines there are two Attributes you can add.
MultilineAttribute
: Multiline strings expand the normal textbox a fixed number of vertical lines. This is less flexible than aTextArea
but can still be useful in some situations.TextArea
: Text Area’s span between a minimum and maximum number of lines and - unlike aMultiline
attribute - they will add scrollbars if the text exceeds the maximum number of lines. Additionally theTextArea
spans the entire width of the Inspector giving more room to type.
The JSON.NET documentation is available here: https://www.newtonsoft.com/json/help/html/Introduction.htm
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u