Using StreamingAssets in Android

What’s up guys,

so I was trying to use JSON databse in my project for Android and I got stopped on a nasty problem:

To use the JSON database I need to load it. On the windows I load it like this:

json_database_items = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + “/Resources/StreamingAssets/Items.json”));

Property that allows me to get straight to the application dirrectory is Application.dataPath.

It doesn’t work on Android though, as app is packed there to .apk and should be used as a .zip archive, so I’ve googled a little and found several advices:

1)Use property Application.streamingAssets
Well, I’ve put the StreamingAssets folder to Assets folder and tried this option. Didn’t work.

2)To use WWW class, for opening database inside of .apk
Didn’t work as well for unknown reason. I put the Items.json straight to assets folder, tried to read ias a string this:

new WWW(“file:java/mnt/asec/companyname.com/pkg.apk!/assets/Items.json”).text;

But it didn’t work for some unknown to me reason, though everything seems correct and java url should have took care about archived file.

I also tryied to use property to use property Application.streamingAssets along with WWW class, but for
some another unknown reasong string returned with that property begins with “jar:file///” and this tripple slash does not seem to be neither working or correct to jar url syntaxis.

Right now I am out of options and I would like to get some help, because it does seem like desperate case here .___.

If you do know that I am wrong someway either made a mistake in code, or there is a simple solution to that task(Doesn’t seem like that and I wouldn’t mind any solution, even the difficult one) please, let me know.

Thx in forward, pals :slight_smile:

P.S. Is there any meaning in trying to use 7zip sdk? If yes, could you please guide me a little in that dirrection?

Unfortunately, @Kiseki_ code didn’t work for me. After long time trying to resolve it, I was able to run the json on android thanks to the code i found on git forums.

this is the code adapt to litjson plugin:

            string filePath = Application.streamingAssetsPath + "/Items.json";
            string jsonString;
            
            if (Application.platform == RuntimePlatform.Android)
            {
                WWW reader = new WWW(filePath);
                while (!reader.isDone) { }
    
                jsonString = reader.text;
            }
            else
            {
                jsonString = File.ReadAllText(filePath);
            }
    
            JsonData itemData = JsonMapper.ToObject(jsonString);

Hope this code help someone, since this problem was driving me crazy.

NEW !!


www is obsolete now,
you should use UnityWebRequest instead,

place your file (ex: jsonFile.json) in StreamingAssets folder and then use the following code, works both in Pc and mobile :

var _path = Application.streamingAssetsPath + "/jsonFile.json";
UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(_path);
www.SendWebRequest();
while (!www.isDone)
{
}
String jsonString = www.downloadHandler.text;

hope this helps,
thank you

Alternatively, it is possible to use the Resources with a TextAsset. Just put your JSON file in a “Resources” folder (in this example: /Resources/MyJSONFolder/config.json)

TextAsset file = Resources.Load("MyJSONFolder/config") as TextAsset;
string content = file.toString();

//Parse your JSON from the content variable   

That works well for me on Android.

Ok, I got it.

To use database(LitJSON plugin) along with unity on android you have to:

1)Put the database in the asset folder (Like: /Assets/Items.json)

2)Load the database via WWW

WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/StreamingAssets/Items.json");

3)Write the database down in a persistent data folder

File.WriteAllBytes(Application.persistentDataPath + "/Items.json", loadDB.bytes);)

Then you can read the database from persistent data folder

json_itemData = JsonMapper.ToObject(File.ReadAllText(Application.persistentDataPath + "/StreamingAssets/" + "Items.json"));

That’s it, it works, it is creepy, but it still does work. I am not kinda good programmer and I do not know the way to read the database straight to the script memory, but you can take a longer trip this way.

Thanks for paying attention

Use Better Streaming Assets