Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Write/Save and Read/Load data to and from json file on Android

Discussion in 'Scripting' started by unity_rUXG6TRGKyfxuQ, Apr 13, 2020.

  1. unity_rUXG6TRGKyfxuQ

    unity_rUXG6TRGKyfxuQ

    Joined:
    Nov 28, 2019
    Posts:
    5
    Hey, everyone.

    I am having trouble saving data to a json file on Android. I did figure out how to load, but not write.
    I have already gone through threads on other places, but I cannot find the solution.

    Here is the code I have for reading/loading that does work:
    Code (CSharp):
    1. public static void ReadDataFromJson() {
    2.         jsonFilePath = Path.Combine(Application.streamingAssetsPath, jsonFileName);
    3.         string dataString;
    4. #if UNITY_EDITOR
    5.         dataString = File.ReadAllText(jsonFilePath);
    6. #elif UNITY_ANDROID
    7.         WWW reader = new WWW(jsonFilePath);
    8.         while (!reader.isDone) { } // Do nothing
    9.         dataString = reader.text;
    10.  
    11.         // Instead of the above lines, I did try using:
    12.         // filePath = Path.Combine(Application.persistentDataPath, jsonFileName);
    13.         // dataString = File.ReadAllText(jsonFilePath);
    14.         // But that didn't work
    15. #endif
    16.         loadedData = JsonUtility.FromJson<SavedData>(dataString);
    17. }
    And here is my code for writing/saving that does not work:
    Code (CSharp):
    1. public static void WriteDataToJson() {
    2.         string dataString = JsonUtility.ToJson(savedData);
    3.         string filePath;
    4.  
    5.  
    6. #if UNITY_EDITOR
    7.         filePath = Path.Combine(Application.streamingAssetsPath, jsonFileName);
    8. #elif UNITY_ANDROID
    9.         filePath = Path.Combine(Application.persistentDataPath, jsonFileName);
    10. #endif
    11.         File.WriteAllText(filePath, dataString);
    12. }
    SavedData is just a class with the data I want to save.

    Thanks for the help and sorry if this is a duplicate thread.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    I'm not seeing issues with the code that creates the save, so you're going to have to explain what exactly isn't working.

    Now, the one issue I'm seeing is with your loading. You don't ever try to load the file once it's saved out. You're always loading from streamingAssetsPath, which is the base file. Even if you save out, you're just loading the base file and not the changed file.

    What you need to do is use File.Exists to check if the file is in PersistantDataPath and if it is, load it up, otherwise load the one from StreamingAssets instead. My guess is that is why you don't think it's working because you never load up the newly created saved file.
     
    unity_rUXG6TRGKyfxuQ likes this.
  3. unity_rUXG6TRGKyfxuQ

    unity_rUXG6TRGKyfxuQ

    Joined:
    Nov 28, 2019
    Posts:
    5
    That helps a lot. I tested it here and it solves my problem.

    Here is my code in case someone has the same trouble:

    Write/Save data:
    Code (CSharp):
    1. public static void WriteDataToJson() {
    2.         string dataString;
    3.         string jsonFilePath = DataPath();
    4.         CheckFileExistance(jsonFilePath);
    5.  
    6.         dataString = JsonUtility.ToJson(savedData);
    7.         File.WriteAllText(jsonFilePath, dataString);
    8. }
    Read/Load data:
    Code (CSharp):
    1. public static SavedData ReadDataFromJson() {
    2.         string dataString;
    3.         string jsonFilePath = DataPath();
    4.         CheckFileExistance(jsonFilePath, true);
    5.  
    6.         dataString = File.ReadAllText(jsonFilePath);
    7.         loadedData = JsonUtility.FromJson<SavedData>(dataString);
    8.         return loadedData;
    9. }
    Set the data path:
    Code (CSharp):
    1. static string DataPath() {
    2.         if (Directory.Exists(Application.persistentDataPath)) {
    3.             return Path.Combine(Application.persistentDataPath, jsonFileName);
    4.         }
    5.         return Path.Combine(Application.streamingAssetsPath, jsonFileName);
    6. }
    Check file existance:
    Code (CSharp):
    1. static void CheckFileExistance(string filePath, bool isReading = false) {
    2.         if (!File.Exists(filePath)){
    3.             File.Create(filePath).Close();
    4.             if (isReading) {
    5.                 SetStartingData();
    6.                 string dataString = JsonUtility.ToJson(savedData);
    7.                 File.WriteAllText(filePath, dataString);
    8.             }
    9.         }
    10. }
    Thanks a lot!
     
  4. Rasmus58

    Rasmus58

    Joined:
    Jun 3, 2019
    Posts:
    23
    İt does not help.
    For example what is SetStartingData() ?
     
  5. isotian

    isotian

    Joined:
    Mar 8, 2015
    Posts:
    6
    Hey,

    I think I does help, but it need some more details to be a complete reference for future problems with JSON

    My questions:
    • Can you show the code of the method SetStartingData()
    • Can you show the code of the class SavedData
    • Where do you run the code? Is this DontDestroyOnLoad? Are savedData and jsonFileName saved global in this class/es?
    • What imports do you use?
    • Since it seems like steamingAssetsPath is not just making problems with Android, but also with WebGL I want to know what are your experiences with this and maybe also with other platforms?
    Looking forward to your help,

    Happy new year!