//SOLVED JSON file can't be found in Application.persistentDataPath

Hi,

So I’m trying to use JSON to save my data instead of Binary Serialization which I’ve used before. It seems easier, but there’s a problem: I can’t find the JSON file, and no data gets loaded.

Here’s the code (some of it unrelated):

using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class TileItems
{
    public Dictionary<Transform, Vector3> Objects = new Dictionary<Transform, Vector3>();
    public string OverWriteThis;
}

public class TilePopulatorV2 : MonoBehaviour
{
    #region "Variables"
    public float MinXRange;
    public float MaxXRange;
    public float MinZRange;
    public float MaxZRange;
    public float TileWidth;
    public TileItems tiles;
    public JSONSaver SaveScript;
    public string JSONString;
    public bool SaveBool;
    #endregion
    // Use this for initialization
    void Start()
    {
        TileWidth = transform.GetComponent<Renderer>().bounds.extents.x;
        LoadData();
    }

    private void Update()
    {
        if(SaveBool)
        {
            SaveData();
            SaveBool = false;
        }
    }

    public void SpawnObject(Transform CurrentObject)
    {
        TileWidth = transform.GetComponent<Renderer>().bounds.extents.x;
        MinXRange = transform.position.x - (TileWidth);
        MaxXRange = transform.position.x + (TileWidth);
        MinZRange = transform.position.z - (TileWidth);
        MaxZRange = transform.position.z + (TileWidth);
        var spawnobj = Instantiate(CurrentObject, new Vector3(UnityEngine.Random.Range(MinXRange, MaxXRange), 0,
                UnityEngine.Random.Range(MinZRange, MaxZRange)), Quaternion.identity, transform);
        spawnobj.localScale = new Vector3(0.2f, 0.2f, 0.2f);
        tiles.Objects.Add(spawnobj.transform, spawnobj.position);
    }

    public void SaveData()
    {
        tiles.OverWriteThis = "TESTING";
        JSONString = JsonUtility.ToJson(tiles);
        Debug.Log("Saved " + Application.persistentDataPath);
    }

    public void LoadData()
    {
        tiles = JsonUtility.FromJson<TileItems>(JSONString);
        Debug.Log("Loaded");
    }
}

My Application.persistentDataPath is C:/Users/ahn20/AppData/LocalLow/Feline Interactive/Forest Explorer 1, but when I go there in the File Explorer, there’s a Unity folder but it’s full on Analytics files. A search for .json gives nothing.

Why does the data seem to save, but doesn’t load, and why isn’t the file being created, or if it is, where is it? Is there something I’m missing in this code?

Thank you!

You have the JSON string, but your not actually saving the data anywhere.

Ouch, the Unity doc is incomplete. I thought it would create the file automatically when calling ToJson.

This does the trick perfectly:

    public void SaveData()
    {
        JSONString = JsonUtility.ToJson(tiles);
        File.WriteAllText(Application.persistentDataPath + "/MyGame.json", JSONString);
        Debug.Log("Saved " + Application.persistentDataPath);
    }

    public void LoadData()
    {
        tiles = JsonUtility.FromJson<TileItems>(File.ReadAllText(Application.persistentDataPath + "/MyGame.json"));
        Debug.Log("Loaded");
    }

Thanks!

2 Likes

Thank you for this!