Creating Json with Unity


Creating Json with Unity

Hello friends, in this article I will talk about how to create a JSON file with Unity and how to add data to it.

Let's move on to how to create a Json file with Unity.
First, open your project and create a C# file with any name you want, I am naming it "JsonOlusturma".


Enter the new C# file we created and create a new class. The reason we create this class is to define the data that will be inside our json file.
Of course, while creating the class, we need to add "[System.Serializable]" on top.

[System.Serializable]

public class Json_Verileri

{

    public string yazi_veri;

    public int sayi_veri;

}


After creating the class, we can start defining the data. The important point here is that the variables we use when defining the data are saved to the json file as they are.

Now let's move on to creating the json file and adding data to it.
What we need to do now is to create a variable of the class we created inside the standard class.
The reason for doing this is to be able to access its elements.

[SerializeField] private Json_Verileri _json = new Json_Verileri();


This is how I access the class I created. Now we need to add to the elements of the class we referenced, we do it as follows.

    private void Awake()

    {

     _json.yazi_veri = "Yazı";

     _json.sayi_veri = 01;

    }


As you can see here, we used the Awake command. The reason we use this is to ensure that these data are added after all classes are read. If we had used the start command instead, since the system reads the codes from top to bottom, in some cases we might encounter errors.

When we have done these operations, the data will be set and all that remains is to save these data as a json file in the system. For this, the following will be sufficient.

 public void Save()

    {

        string to_save = JsonUtility.ToJson(_json);

        System.IO.File.WriteAllText(Application.dataPath + "JsonDosyasi".json", to_save);      

    }

We created a function named Save and performed the operations inside it.
With the string to_save part, we converted the class elements to text and, thanks to Unity's json tool, into json format, then by specifying the file location with the file creation process, we added the json file we created into the created file.

By the way, friends, "Application.dataPath" ensures that your saved file is stored among the game files. (In the Editor, into the Assets folder, when you build, into the folder where the game is installed.)
If you use "Application.persistentDataPath", the created json file is saved to
C: => Users => [user] => AppData => LocalLow => [company name]
location.

Note: To save into any folder you create inside the game file or locallow, you just need to change the "JsonDosyasi" part above to "FolderName/JsonDosyasi" and write the name of the folder you created where it says Folder Name.

If you have done everything correctly up to here, you can now add a button to your game and call the Save function to create your json file.

Our file should look like this in the end.
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class JsonOlusturma : MonoBehaviour

{

    [SerializeField] private Json_Verileri json = new Json_Verileri();

    private void Awake()

    {

        json.yazi_veri = "Text";

        json.sayi_veri = 15;

    }

    public void Save()

    {

        string to_save = JsonUtility.ToJson(json);

        System.IO.File.WriteAllText(Application.dataPath + "/Kayıt/" + "save" + ".json", to_save);


    }

}



[System.Serializable]

public class Json_Verileri

{

    public string yazi_veri;

    public int sayi_veri;

}

The generated json file will look like this.

{

  "yazi_veri": "Text Data",

  "sayi_veri": 1
}
If you have any questions about json creation, you can write them below as comments.