0

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

Sarah
  • 135
  • 3
  • 19

2 Answers2

4

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.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
-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!

  • This doesn't address the issue of not being able to access the json file. In fact it repeats code she already posted as not working for her. – shox Jun 15 '20 at 22:37
  • You need to provide any limitations, assumptions or simplifications in your answer. See more details on how to answer at this link: https://stackoverflow.com/help/how-to-answer – Usama Abdulrehman Jun 15 '20 at 22:45
  • @shox, you're not right. In her post contains the wrong code. `string jsonString = file.ToString ()` - will not work – Alison FirstBlood Jun 16 '20 at 00:35
  • @Александр Пономарев it's functionally equivalent and if you read the question that isn't the issue. Her code works on desktop, she can't access the file on Android. You failed to address the problem. – shox Jun 16 '20 at 01:37
  • @shox You will not be able to access JSON content if you use ToString for an instance of TextAssets. I have already tested this code, as I have the SAME problem. But probably you didn’t check it, condemning me and making an ethical mistake. – Alison FirstBlood Jun 16 '20 at 12:55