[SOLVED] Converting a Generic List to JSON in Unity

Hello programmers,

I am a beginner and I searched and tried some solutions, but it didn’t fit / solved my case.

I guess this is simple, but I didn’t came to an answer yet. Hope you guys could help me.

Ok, I have this Class and Object for a 2D Tile Map creator:

public class Columns
    public int[] lines;

public Columns[] tilesMap;

After putting some numbers to the ‘int’ lists I wanted to save it as a .JSON.

I followed the tutorial, that works fine with others classes, but when I did this:

string dataAsJson = JsonUtility.ToJson(tilesMap);
string filePath = Application.dataPath + "/Maps/" + generatedLevelFileName + ".json";
File.WriteAllText(filePath, dataAsJson);

I simply, get this:

Example File: “Application.dataPath/Maps/level1.json”


Thanks in advance!

I found a great way to serialize generic lists. This works at least on Unity version 2020.3.

using System.Collections.Generic;
using UnityEngine;

public class SerializableList<T> {
    public List<T> list;

public class ListToJsonExample : MonoBehaviour {
    [SerializeField] private SerializableList<string> names;

    private void Awake() {

        string json = JsonUtility.ToJson(names);




I am having the same problem. My code is a bit longer, but I am trying to save the names of my gameobjects in a Json formatted list… I’m having no luck figuring out what my problem is on the forums. When I go to convert the list, Json just returns “{ }”. Here is my code:

public class SaveLoad : Inventory {

private static GameObject player;
private static System.Collections.Generic.List<string> Wpns = new System.Collections.Generic.List<string>();
private static System.Collections.Generic.List<string> Cnsm = new System.Collections.Generic.List<string>();
private static System.Collections.Generic.List<string> Keys = new System.Collections.Generic.List<string>();
private static System.Collections.Generic.List<string> Placeholder = new System.Collections.Generic.List<string>();
private static System.Collections.Generic.List<int> PlySas = new System.Collections.Generic.List<int>();
private static System.Collections.Generic.List<int> IsoSas = new System.Collections.Generic.List<int>();
private static string nameHolder;

private static string json;

private static string SAVE_FILE;

protected  static void Save() {

SAVE_FILE = Stats.PlayerName + ".json";
    /////Convert each to list/////
    if (Wpns != null)
    Wpns = ToList(Inventory.Weapons);
    SaveJSON("Weapons", Wpns);
    if (Keys != null)
    Keys = ToList(Inventory.KeyItems);
    SaveJSON("KeyItems", Keys);
    if (Cnsm != null)   
    Cnsm = ToList(Inventory.Consumables);
    SaveJSON("Consumables", Cnsm);

    ////Save in as JSON////

private static System.Collections.Generic.List<string> ToList(GameObject[] array)
    if (Placeholder != null)
    Placeholder = new System.Collections.Generic.List<string>();
    for (int i = 0; i <= (array.Length - 1); i++)
        if (array*)*

nameHolder = array*.name;*
return Placeholder;
private static void SaveJSON(string type, System.Collections.Generic.List list )
json = JsonUtility.ToJson(list);
print(json + " " + type);
string filename = Path.Combine(Application.persistentDataPath, type + SAVE_FILE);
if (File.Exists(filename)) {
File.WriteAllText(filename, json);

i wanted to convert the list to JSON so i did something like this

public class _Recentplayers
    public List<_UserDetails> m_recent_players;

public class _UserDetails
    public string m_username;
    public string m_my_name;

And then

            m_users = new _Recentplayers();
            m_ud = new _UserDetails();

            m_ud.m_my_name = "";
            m_ud.m_user_name = "";

            string json = JsonUtility.ToJson(m_users);