#Saving/Loading Json

1 messages ยท Page 1 of 1 (latest)

frosty roost
#

Here's my script

#

Crap sent it wrong

#

I'll get to my pc

teal flare
#

Use !code

opaque ventureBOT
teal flare
#

Anyway. JsonUtility can't save a dictionary. You need a serializable dictionary or a different json serializer

frosty roost
#

Ok. In about 15 minutes I'll be at my pc. I created a testing app using the same methods and it works.

#

I just think it's data types or not getting the data values

frosty roost
#

ok at my pc

#

Ok my first question would be do you see anything wrong with how I'm handling my json saving/loading

#
// For saving/loading player data when no internet connection is found
private string GetSaveFilePath()
{
    // Define the file path for saving data
    return Path.Combine(Application.persistentDataPath, "OfflinePlayerData.json");
}
#
public void SaveDataOffline()
{
    playerSaveData = new()
    {
        CorrectAnswers = _correctAnswers,
        WrongAnswers = _wrongAnswers,
        FastestSavedTime = _fastestTime,
        FiftyFiftyCount = _fiftyFiftyCount,
        AudiencePollCount = _audeiecePollCount,
        SkipQuestionCount = _questionSkipCount,
        PlayerGemCount = _playerGemCount,
        NumberOfWatchedAds = _numberOfAdsWatched,
        RemoveAds = _removeAdsPurchased,
        DailyRewardTimestamp = _dailrewardsTimestamp,
        SetBackground = _setBackground,
        User = AuthenticationManager.Instance.playerUserNameDisplay.text,
    };

    // Serialize SaveData to JSON
    string jsonData = JsonUtility.ToJson(playerSaveData);
    File.WriteAllText(GetSaveFilePath(), jsonData);

    Debug.Log($"Data saved to file: {GetSaveFilePath()}");
}
#
public void LoadDataOffline()
{
    string filePath = GetSaveFilePath();

    if (File.Exists(filePath))
    {
        // Read JSON from file
        string jsonData = File.ReadAllText(filePath);
        playerSaveData = JsonUtility.FromJson<SaveData>(jsonData);

        // Update variables from loaded data
        _correctAnswers = playerSaveData.CorrectAnswers;
        _wrongAnswers = playerSaveData.WrongAnswers;
        _fastestTime = playerSaveData.FastestSavedTime;
        _fiftyFiftyCount = playerSaveData.FiftyFiftyCount;
        _audeiecePollCount = playerSaveData.AudiencePollCount;
        _playerGemCount = playerSaveData.PlayerGemCount;
        _questionSkipCount = playerSaveData.SkipQuestionCount;
        _numberOfAdsWatched = playerSaveData.NumberOfWatchedAds;
        _removeAdsPurchased = playerSaveData.RemoveAds;
        //_dailrewardsTimestamp = (Timestamp)playerSaveData.DailyRewardTimestamp;
        _setBackground = playerSaveData.SetBackground;
        _userName = playerSaveData.User;
        _profilePicURL = playerSaveData.ProfilePicURL;

        Debug.Log($"Data loaded from file: {filePath}");

        UpdateUI();
    }
    else
    {
        Debug.Log("No save file found.");
    }
}
#

this script is my test run I made this morning and it works flawlessly

public class Save_Loading_Json : MonoBehaviour
{
    [Serializable]
    public class UserData
    {
        public string firstName;
        public string lastName;
        public string age;
    }

    public TMP_InputField firstNameInputField;
    public TMP_InputField lastNameInputField;
    public TMP_InputField ageInputField;

    private string saveFilePath;

    void Start()
    {
        // Define the path where the JSON file will be saved
        saveFilePath = Path.Combine(Application.persistentDataPath, "userData.json");

        // Load data on start, if it exists
       // LoadData();
    }



    // Save data to JSON
    public void SaveData()
    {
        UserData data = new UserData();
        data.firstName = firstNameInputField.text;
        data.lastName = lastNameInputField.text;
        data.age = ageInputField.text;

        string jsonData = JsonUtility.ToJson(data, true);
        File.WriteAllText(saveFilePath, jsonData);

        Debug.Log("Data saved to " + saveFilePath);
    }



    // Load data from JSON
    public void LoadData()
    {
        if (File.Exists(saveFilePath))
        {
            string jsonData = File.ReadAllText(saveFilePath);
            UserData data = JsonUtility.FromJson<UserData>(jsonData);

            firstNameInputField.text = data.firstName;
            lastNameInputField.text = data.lastName;
            ageInputField.text = data.age;

            Debug.Log("Data loaded from " + saveFilePath);
        }
        else
        {
            Debug.Log("No save file found.");
        }
    }
}
stoic ridge
#

It looks like you are still trying to parse dictionaries through JsonUtility