Search Unity

best practices for android json use?

Discussion in 'Android' started by keblight, Jul 7, 2019.

  1. keblight

    keblight

    Joined:
    May 27, 2015
    Posts:
    34
    hi, just wondering what the procedure is for reading/writing to a file using json for an Android build. I have no issues with this in the editor but of course in my build it seems that nothing at all is being read/written. I've tried pre-processor directives. here is an example ...
    Code (csharp):
    1.  
    2. public svo loadLevelData()
    3.     {
    4.         #if UNITY_ANDROID
    5.         path = Path.Combine(Application.streamingAssetsPath+"/",PlayerPrefs.GetInt ("lastLevelPlayed").ToString());
    6.             return JsonUtility.FromJson<svo>(path);
    7.         #endif
    8.  
    9.         #if UNITY_EDITOR
    10.         return JsonUtility.FromJson<svo>("Assets/resources/saves" + (PlayerPrefs.GetInt ("lastLevelPlayed") ).ToString());
    11.         #endif
    12.     }
    13.  
    svo is a class which is marked as serializable, it just holds some data...
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [System.Serializable]
    6. public class svo {
    7.     public string grade;
    8.     public int goldFound,treasureFound;
    9.     public bool hasTakenDamage;
    10.     public float time;
    11. }
    12.  
    application.persistantDataPath works in editor too but not when I try to run the build on my phone, do I have to use the WWW class to do this? If anyone has any incite it would be much appreciated, thanks!
     
    Last edited: Jul 7, 2019
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
  3. keblight

    keblight

    Joined:
    May 27, 2015
    Posts:
    34
    hey, thanks for the reply. I edited the post, also checked the write permission...it is set to internal, the other option is external(sd card). I have tried both persistantDataPath and streaming assets, both aren't working with the way I have things set up. I'm thinking this is a simple fix but just not sure how to get it working on android. I've had similar issues with android stuff in the past, so annoying, it just slows things right down.
     
  4. keblight

    keblight

    Joined:
    May 27, 2015
    Posts:
    34
    still not working, have been looking for a real answer. Seems this is another common problem that still hasnt been clearly addressed.
     
  5. keblight

    keblight

    Joined:
    May 27, 2015
    Posts:
    34
    got it working, the issue was the path... use Path.Combine( application.persistentDataPath, yourFileName + ".json"); the extension .json seemed to get it working. Also, I had the logic that wrote the json file in a "save" method that was being called form another class but I did not have the path variable declared within the save method itself, I had it above, in start. I assume start was not being called, only the save method... So I was also getting errors saying that the path was NULL, anyway, its all fixed now :)
     
    Last edited: Jul 12, 2019