LitJSON in Unity3D

Creating databases for our game seems to be a difficult task, but LitJSON makes it really easy. Today we will take a look at simple method of reading  JSON files.

LitJSON is a small and fast library for handling data in the JSON format. It is written in C# and is compatible with all .Net languages.

First lets create a JSON file. We can use VisualStudio Community Version or VS Express for Web. To crate Json file you can simply create new text file on your computer and change extension to from .txt to .json.

Now lets open the file in our VS. To demonstrate the example I created list of ships that will be used in my game. So here is the basic layout of the file:

//Arrays are marked by [].
[
 {
 "agility": 3,
 "armor": 300,
 "cargo": 15,
 "details": "complete the game helping race 1 to unlock ",
 "maxSpeed": 3,
 "weight": 1100
 }, //remember to type comma after every element, except last one
 {
 "details": "complete the game helping race 2 to unlock ",
 "armor": 100,
 "maxSpeed": 2,
 "agility":5,
 "cargo": 10,
 "weight": 800 // no comma here

 }
]

JSON can store intagers and strings. Key signatures are written between “” and valu is stored after colon( : ) elemants are separated by commas.

 

Here is our c# Script

You need to add LitJson liblary, you need to download the dll file first and than add the reference to the project in VS referance manager.

using LitJson;
// We also need System.IO to read the files.
using System.IO;
public class ShipSelection : MonoBehaviour {

/we declare two variables, string and JsonData.
 private string JSONstring;
 private JsonData stateData;

 void Start () {
 
// Here we read our file from our game assets as a string field.
 JSONstring = File.ReadAllText(Application.dataPath + "/JSON/ShipsDetails.json");
 Debug.Log(Application.dataPath + "/JSON/ShipsDetails.json");
// Now w convert it to JsonData object
 stateData = JsonMapper.ToObject(JSONstring);
 }

Now when you have referance to the object we can read the selected property from it. To do so lets use this line of code

panelText.text = stateData[ship_number]["details"].ToString();

panelText is a reference to an object on the scene;

ship_number is an integer value, defined somewhere in the code: int ship_number =0;

Lets analyze the structure of this code.

To pass the text value we need to convert our data to back to string we use build in method - .ToString()

we use the name of our JsonData object than we select the object Id, in our example we have two objects with indexes 0 and 1. Indexes starts with 0. Now when we are inside our selected object we choose the property wo want to use, lets say “details”.

This will select and display the following

   complete the game helping race 1 to unlock

We can select the int values as well

   SHIP_STATS[0]= (int)stateData[ship_number]["armor"];

We store the value of ship armor in array of inegers, we can use it as int value and perform math operations on it.

This is very basic example, when we are not creating classes for the objects. This topic will be discused in next post. Have fun with your new tool. And as always Stay Awesome!