Question by
BLGO94 · Nov 12, 2019 at 07:28 PM ·
androidandroid buildjsonreading datawrite data
Android json read and write
Hi, i'm having a problem using and Json to store data.
I'm using this methods to read and write an json. They work fine, but in the android build nothing happens.
void Read()
{
string path = "Assets/Resources/patients-data.json";
StreamReader reader = new StreamReader(path);
string data_text = reader.ReadToEnd();
//Debug.Log(data_text);
PatientData data = JsonUtility.FromJson<PatientData>(data_text);
patients_data = data;
reader.Close();
}
void SaveData()
{
string path = "Assets/Resources/patients-data.json";
StreamWriter writer = new StreamWriter(path, false);
string jsonData = JsonUtility.ToJson(patients_data, true);
writer.Write(jsonData);
writer.Close();
}
I tried StreamingAssets to but it failed.
Comment
Answer by PizzaPie · Nov 12, 2019 at 08:57 PM
The build, as it should be obvious, does not contain the same directories/ files as the project. You ll need to change the path for any build, not just android, to work. Most common location used is Application.persistentDataPath, but you may use any other directory you want.
You should consider using Android monitor, or any equivalent software, with development build checked to debug android builds or hook the apk to the Editor.