Sarah
Sarah

Reputation: 135

Loading a JSON file to android device in Unity3D

I need to load my json file in my device. It is working perfectly fine in Unity Editor but when I transferred it to my device, json isn't there. Currently using this code for adding data in my json file:

TextAsset file = Resources.Load("Products") as TextAsset;
    string jsonString = file.ToString ();
    Debug.Log (jsonString);
    if (jsonString != null)
    {
        jsonParser data = JsonUtility.FromJson<jsonParser>(jsonString);

        data.products.Add(product);
        Debug.Log(data.products[1].code);

        jsonString = JsonUtility.ToJson(data);
        Debug.Log(jsonString);
    }

And this for retrieving:

TextAsset file = Resources.Load("Products") as TextAsset;
    string json = file.ToString ();
    //string path = Application.streamingAssetsPath + "/Products.json";
    string path = Application.persistentDataPath + "/Resources/Products.json";
    string jsonString = File.ReadAllText (path);

    jsonParser data = JsonUtility.FromJson<jsonParser>(jsonString);

It can be use in Unity editor but not in my android device. What seems to be the problem? Very big thanks

Upvotes: 0

Views: 7108

Answers (2)

Alison FirstBlood
Alison FirstBlood

Reputation: 1

Another simple variant, if you ONLY want to get JSON's text from Resourse:

1.⠀You have file in Resource/Jsons folder, example "simple.json"
2.⠀You use this code:

TextAsset file = Resources.Load("Jsons/simple") as TextAsset;
string json = file.text;

3. Now, you have JSON in string variable!
4. Thats all! Glory to Robots!

Upvotes: -1

Programmer
Programmer

Reputation: 125455

As explained here, you can't read file from the Resources folder with anything other than Resources.Load. You must use Resources.Load to read anything placed in the Resources folder.

It doesn't matter if it works in the Editor or not the code below that reads from the Resources folder should not work in any build:

string path = Application.persistentDataPath + "/Resources/Products.json";
string jsonString = File.ReadAllText (path);

You don't seem to understand when the Resources folder should be used. This is used to store files that does not need to be changed during run-time. For example, your textures, sound, video files and prefabs. These are not meant to be modified during run-time Also, using it to store default value for a text file is fine.

What need to do is to read the file with Resources.Load("Products") then store it in a string. After you modify it, save to another directory with

Save:

string tempPath = Path.Combine(Application.persistentDataPath + "/data/", dataFileName + ".txt");
File.WriteAllBytes(tempPath, modifiedJson);. 

Load:

string tempPath = Path.Combine(Application.persistentDataPath + "/data/", dataFileName + ".txt");
jsonByte = File.ReadAllBytes(tempPath);

I made a generic class in another question, to handle that easily so you that you don't even need to use the Resources folder or read the file manually. It handles all the errors too.

From your question, your data is stored in jsonParser class. You should rename that class to actually reflect what you are storing such as PlayerInfo or ProductInfo.

Grab the DataSaver class from here. With your current jsonParser data, you can save and load with the code sample below:

Save:

jsonParser products = new jsonParser();
DataSaver.saveData(products, "Products");

Load:

jsonParser loadedProducts = DataSaver.loadData<jsonParser>("Products");

Delete:

DataSaver.deleteData("Products");

Very easy. That should work on any Platform.

Upvotes: 4

Related Questions