Saving a list into a json with DateTime as a Header

i don’t know if I’m explaining it right but what i want is to be able to save 3 lists of strings but have them separated based on time. The idea is to store some strings for each day and save them into a json so i can search for the date and display them into a calendar.

My try was like so

[Serializable]
public class TimeStamp
{
   public string CreatedDate;
   public List<Entries> day = new List<Entries>();
}

[Serializable]
public class Entries
{
   public List<string> names = new List<string>();
   public List<string> location = new List<string>();
   public List<string> info = new List<string>();
}

which then i saved like so

 string json = JsonUtility.ToJson(timeStamp);
       try
       {
           timeStamp.CreatedDate = DateTime.UtcNow.ToLocalTime().ToString("dd-MM-yyyy   HH:mm");
           File.WriteAllText(Application.persistentDataPath + "/Journal.json", json);
           Debug.Log("Data Saved");
       }

       catch (Exception e)
       {
           Debug.Log("Error Saving Data:" + e);
           throw;
       }

Which does save everything i need but its not separated by the date, trying to explain it the way i think of it is that i want the date to be the “header” of the Json so in one Json file i would have all entries for each day and also i made them into a list so i can add or remove strings.

So later on when i want to add something i would search for the date and access the names list for instance and .Add() my string to that list

In a rather crude way this is how i would like my json to look like:

entries
[
   "date"[
          list1 = []
          list2 = []
          list3 = []
        ]       
]

The way i feel like it should be is something like this

[Serializable]
public class Roots
{
   public TimeStamp timeStamp;
}

[Serializable]
public class TimeStamp
{
   public List<Entries> entries;
}

[Serializable]
public class Entries
{
   public List<string> names = new List<string>();
   public List<string> location = new List<string>();
   public List<string> info = new List<string>();
}

which gives a json like this

{
  "Entries": {
   "Timestamp": [ // this entry here //
     {
       "names": "",
       "location": "",
       "info": ""
     }
   ]
  }
}

but how can i make that Timestamp be a string or a date?

System.DateTime can help you with almost all time / date needs, including formatting.

Problems with Unity “tiny lite” built-in JSON:

In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window → Package Manager).

https://forum.unity.com/threads/jso…-not-working-as-expected.722783/#post-4824743

Also, always be sure to leverage sites like:

https://csharp2json.io

PS: for folks howling about how NewtonSoft JSON .NET will “add too much size” to your game, JSON .NET is like 307k in size, and it has the important advantage that it actually works the way you expect a JSON serializer to work in the year 2021.

thank you for the reply , i ended up working it like this

[Serializable]
public class Roots
{
    public int dayCount = -1;
    public List<TimeStamp> timeStamps;
}

[Serializable]
public class TimeStamp
{
    public string CreatedDate = DateTime.UtcNow.ToLocalTime().ToString("dd-MM-yyyy   HH:mm");
    public Entries day;
}

[Serializable]
public class Entries
{
    public List<string> name = new List<string>();
    public List<string> location = new List<string>();
    public List<string> info = new List<string>();
}

public class JournalData : MonoBehaviour
{
    [SerializeField] private Roots root;
    public Roots Roots { get => root; }


    public void ChangeDay()
    {
        root.dayCount++;
        root.timeStamps.Add(new TimeStamp());
    }

    void AddToList()
    {
        root.timeStamps[root.dayCount].day.name.Add("John");
    }


    public void Save()
    {
        string json = JsonUtility.ToJson(root);
        try
        {
            File.WriteAllText(Application.persistentDataPath + "/Journal.json", json);
            Debug.Log("Data Saved");
        }

        catch (Exception e)
        {
            Debug.Log("Error Saving Data:" + e);
            throw;
        }
     
    }

    public void Load()
    {
        try
        {
            string userString = File.ReadAllText(Application.persistentDataPath + "/Journal.json");

            Roots rootData = JsonUtility.FromJson<Roots>(userString);
         
            if (rootData != null)
            {
                root.dayCount = rootData.dayCount;
                root.timeStamps = rootData.timeStamps;
            }
         
            Debug.Log("Data Loaded");
        }
        catch (Exception e)
        {
            Debug.Log("Error Loading Data:" + e);
            throw;
        }
    }

}

which generates this JSON

{
    "dayCount": 0,
    "timeStamps": [
        {
            "CreatedDate": "20-08-2021   13:14",
            "day": {
                "name": [
                    "John"
                ],
                "location": [],
                "info": []
            }
        }
    ]
}

but since its obvious you know more about it than me what do you think of this way? is it good or should i switch to newtonsoft and let that handle things?

If it works, ship it!

If you understand the limitations and Unity’s JSON Lite still solves your problem, go with what you have. Just know when it will stop working.