JSON error: Invalid value when loading file

Heya!

I’m trying to put together a save/load character system for my game, and I keep running into this error. I’ve made a fresh project to just test out the saving/loading data to try and focus in on the problem.

What I’m trying to achieve: players can set a few values (strings and ints) for their character, then save it out and load it in later. They do so by pressing a “save” button and a “load” button respectively, which call functions on the “CharInGame” script. For testing purposes, you just enter the character stats into the inspector.

What is happening: saving seems to be ok, but loading results in the error:

ArgumentException: JSON parse error: Invalid value.
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at <7a84dc901f21458f98a12e112d4ba2ab>:0)
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at <7a84dc901f21458f98a12e112d4ba2ab>:0)
CharInGame.LoadCharacter () (at Assets/CharInGame.cs:32)
UnityEngine.Events.InvokableCall.Invoke () (at <fe84f4a754da4a6bb64fca409d40938a>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <fe84f4a754da4a6bb64fca409d40938a>:0)
UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventS

Here are my scripts:

My Character Data class, “CharData”:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class CharData
{
    public string VarName;
    public int Power;
    public int Speed;
    public string Catchphrase;
}

The script that handles saving and loading files:
using System.Collections;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CharInGame : MonoBehaviour
{
    public CharData Character;
    public string saveFile;

    public void SaveCharacter()
    {
        string jsonString = JsonUtility.ToJson(Character);
        Debug.Log(jsonString);
        saveFile = Application.persistentDataPath + "/CharData.json";
        File.WriteAllText(saveFile,jsonString);
    }
    public void LoadCharacter()
    {
        CharData LoadedCharacter = JsonUtility.FromJson<CharData>(saveFile);
    }
}

The output I am getting from “Debug.Log(jsonString);” is (depending on my input) {"VarName":"Bill","Power":5,"Speed":10,"Catchphrase":"Thats a lotta spiders!"}"

Hope that is enough info!

Found the problem - I was parsing the string of the directory itself as the json data! Changed my load function to:

public void LoadCharacter()
    {
        string jsonToRead = File.ReadAllText(Application.persistentDataPath + "/CharData.json");
        CharData LoadedCharacter = JsonUtility.FromJson<CharData>(jsonToRead);
    }

Cheers!