write to json file on Android

hello
i am trying to save data on json file on Android device.

i use this code to read the file:

    string filePath = Application.streamingAssetsPath + "/data.json";
    string dataAsJson;

    if (Application.platform == RuntimePlatform.Android)
    {
        WWW reader = new WWW(filePath);
        while (!reader.isDone) { }

        dataAsJson = reader.text;

    }
    else
    {
        dataAsJson = File.ReadAllText(filePath);
    }

    myData = JsonUtility.FromJson<data>(dataAsJson);

and this code to save the file

    dataAsJson = JsonUtility.ToJson (myData);
    File.WriteAllText (filePath, dataAsJson);

Sorry, my bad. It should be: float score = PlayerPrefs.GetFloat("Highscore"); score -= item.Value; @Ashmit2020 ---------- To save what the player bought, you might want to use something a little different to PlayerPrefs. You could save an individual string for each item into playerPrefs, but it would be easier to write to a text file, containing the contents of what you bought, then reading it back again, when loading your inventory. ---------- You're asking a lot of questions, and it would be easier to answer individual questions. Maybe you could post another question?

–

1 Answer

1

I was able to use Application.persistentDataPath and avoid using WWW altogether. For example :

Save Data (list of custom class objects, questList)

 string filePath = Path.Combine(Application.persistentDataPath, "currentBaseQuests.json");
 string jsonData = JsonUtility.ToJson(_questList);
 File.WriteAllText(filePath, jsonData);

Load Data (list of custom class objects)

string filePath = Path.Combine(Application.persistentDataPath, "currentBaseQuests.json");
string loadData = File.ReadAllText(filePath);
return JsonUtility.FromJson<CurrentBaseQuestClass>(loadData);

Sry for asking too much I am a newbie

–