0

How read json from StreamingAssets for android app?

For iOS I read with this code,

public void ReadJson(){
        string path = "/Raw/elements.json";
        #if UNITY_EDITOR
        path = "/StreamingAssets/elements.json";
        #endif
        string json = File.ReadAllText(Application.dataPath + path);
        itemsContent = JsonUtility.FromJson<Main> (json); 
        bundleForPing = itemsContent.bundleID;
    }

but how read for android ... please help

Abs3akt
  • 387
  • 4
  • 19

1 Answers1

2

Based on this Manual:

On Android, the files are contained within a compressed .jar file (which is essentially the same format as standard zip-compressed files). This means that if you do not use Unity’s WWW class to retrieve the file, you need to use additional software to see inside the .jar archive and obtain the file.

public void ReadJson(){
        string path = Application.streamingAssetsPath + "/elements.json";
        #if UNITY_EDITOR
        path = "/StreamingAssets/elements.json";
        #endif
        WWW www = new WWW(path);
        while(!www.isDone) {}
        string json = www.text;
        itemsContent = JsonUtility.FromJson<Main> (json); 
        bundleForPing = itemsContent.bundleID;
    }
Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48
  • I'm no expert but I'm sure I read something about issues with WWW, including memory issues. After some time spent on finding an alternative to the use of "exception" based on context, which add IMO unneccesary complexity I found this plugin https://assetstore.unity.com/packages/tools/input-management/better-streaming-assets-103788 Then you can do something like that: `code` BetterStreamingAssets.Initialize(); MyConfigClassInstance = JsonUtility.FromJson( BetterStreamingAssets.ReadAllText("config_file.json") ); – job3dot5 Mar 21 '19 at 08:34