Load Json net file

I managed to save my values to a Json file using Json net, using the following script:

using System.IO;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public static class SaveManager 
{
    public static string directory = "/TapJoy/";
    public static string fileName = "MyData.txt";

    public static void Save()
    {     
        string dir = Application.persistentDataPath + directory;
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }

        using (FileStream stream = new FileStream(dir + fileName, FileMode.Create, FileAccess.Write))
        {
            using (StreamWriter writer = new StreamWriter(stream))
            {
                string jsonString = JsonConvert.SerializeObject(SaveData.pageList, Formatting.Indented);
                writer.Write(jsonString);
            }
        }
    }

    public static SaveData Load()
    {
        string fullPath = Application.persistentDataPath + directory + fileName;
        SaveData sd = new SaveData();

        if(File.Exists(fullPath))
        {        
             string json = File.ReadAllText(fullPath);
            sd = JsonSerializer.Deserialize<SaveData>(json);
        }
        else
        {
            Debug.Log("Save File does not Exist");
        }
        return sd;
    }  
}

This all works, I have a json file as seen in the screenshot, however I am struggling with loading the file in order to retrieve the values, could someone lend me a hand and give me some hints on how to achieve this ?

Use Unity JSON Parser and Parse each value then convert them into Int or string

You’re doing something strange here. When you serialize your data you use

JsonConvert.SerializeObject(SaveData.pageList, Formatting.Indented);

but when deserializing you use

sd = JsonSerializer.Deserialize<SaveData>(json);

This makes no sense. When you serialize you only pass in your list of pages and therefore you get an array serialized in the json as expected. However when you deserialize your data you suddenly expect a SaveData object which is not what the json represents.

I’ve seen your other post where you’ve shown your SaveData class. Why is your pageList static? Static variables are not serialized. If you never intented to serialize the SaveData class, that’s fine. In this case you just have to deserialize your json like this:

SaveData.pageList = JsonSerializer.Deserialize<List<SaveData.Pages>>(json);

Though It would make more sense to actually serialize your SaveData class in case you need to save more than your page list. Though for that your pageList need to be nonstatic.

Currently this line is pretty pointless:

SaveData sd = new SaveData();

since your SaveData class is completely empty. It doesn’t have a single instance field.

I would suggest you use something like this:

[Serializable]
public class Piece
{
    public int id;
    public bool isColored;
    public float color_r;
    public float color_g;
    public float color_b;
    public float color_a;
    public Piece(bool colored, Color col)
    {
        this.isColored = colored;
        this.color_a = col.a;
        this.color_b = col.b;
        this.color_g = col.g;
        this.color_r = col.r;
    }
}

[Serializable]
public class Page
{
    public string name;
    public float timer;
    public int starRating;
    public int failCounter;
    public List<Piece> pieceList = new List<Piece>();
}

[System.Serializable]
public class SaveData
{
    public static SaveData instance = new SaveData();
    public List<Page> pageList = new List<Page>();
}

Now we can actually work with a SaveData instance. I’ve added a “singleton like” static variable so it’s easier to fit your current logic. Though I would recommend that you actually pass the SaveData instance you want to save to your Save method. Anyways, to save your current data you would do

string jsonString = JsonConvert.SerializeObject(SaveData.instance, Formatting.Indented);

to deserialize you use

SaveData.instance = JsonSerializer.Deserialize<SaveData>(json);

Of course your json text would look different now. We now actually serialize an object as root element, not a list. So it would be something like:

{
  "pageList": [
    {
      "name": "Page1",
      ........
    }
  ]
}

Note: I changed your class names from plural to singular since a “Piece” instance represents one piece and not pieces. Likewise a Page is a single page and not multiple pages. You rarely use the plural for classnames. The only exception would be when you create a collection class. i.e. a specialized form of List for a particular element type

public class GameObjects : List<GameObject>
{
    // [ ... ]
}

Though this is not very common. If you create a specialized List for GameObjects you would usually call it GameObjectList.