0

Hello I have a question on how I can read the data from a JSON file I have already made the saving method I just can't seem to find out how to read JSON files

Code for my saving system:

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GlobalControl : MonoBehaviour
{
public GameObject Player;

public void Save()
{
    var player = Player.GetComponent<PlayerScript>();

    PlayerData playerdata = new PlayerData();
    playerdata.pos = player.transform.position;
    playerdata.curreceny = player.currency;
    playerdata.playerDebug = player.IsUsingDebug;

    string json = JsonUtility.ToJson(playerdata);
    Debug.Log("Player data has been saved");

    File.WriteAllText(Application.dataPath + "playerData.json", json);
    PlayerData loadedPlayerData = JsonUtility.FromJson<PlayerData>(json);
}



public class PlayerData
{
    public bool playerDebug;
    public Vector3 pos;
    public int curreceny;
}
}
YOIIII
  • 1
  • so what is the problem you face? To load you should load the file into a string and Deserialize it to PlayerData again. What about File.ReadAllText? – Starbax Sep 23 '20 at 22:08
  • Does this answer your question? [Easiest way to read from and write to files](https://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files) – Ruzihm Sep 23 '20 at 22:11

1 Answers1

0

I happened to be implementing that on my application currently i would suggest that you add this JSON file in StreamingAssets Folder inside your project then access it

this is my function to read it

        string filePath = Path.Combine(Application.streamingAssetsPath, jsonfileName);
        WWW www = new WWW(filePath);
        while (!www.isDone) { }
        string dataAsJson = www.text;
        OnRead(dataAsJson);

and this is the full class

using UnityEngine;
using System;
using System.IO;
using System.Threading.Tasks;

public class StreamingDataHandler
{
    private static StreamingDataHandler Inistance
    {
        get
        {
            if (inistance == null) inistance = new StreamingDataHandler();
            return inistance;
        }
    }
    static StreamingDataHandler inistance;

    private StreamingDataHandler()
    {

    }

    public static void ReadStringFile(string jsonfileName, Action<string> OnRead, bool isAsync = true)
    {
        Inistance.ReadFile(jsonfileName, OnRead, isAsync);
    }

    private void ReadFile(string jsonfileName, Action<string> OnRead, bool isAsync = true)
    {
        string filePath = Path.Combine(Application.streamingAssetsPath, jsonfileName);
        if (isAsync)
        {
            ReadStringFileAsync(filePath, OnRead);
        }
        else
        {
            string filePath = Path.Combine(Application.streamingAssetsPath, jsonfileName);
            WWW www = new WWW(filePath);
            while (!www.isDone) { }
            string dataAsJson = www.text;
            OnRead(dataAsJson);
        }
    }
    private async void ReadStringFileAsync(string filePath, Action<string> OnRead)
    {
        WWW www = new WWW(filePath);
        while (!www.isDone)
        {
            await Task.Yield();
        }
        string dataAsJson = www.text;
        OnRead(dataAsJson);
    }


}