0

My problem is that the JSON file is not loaded/read in android device, only in unity editor. I try many solutions from many sites, like this, this, and this, but nothing happen.

So anyone can help me. This is my code:

private void LoadGameData()
{
    // Path.Combine combines strings into a file path
    // Application.StreamingAssets points to Assets/StreamingAssets in the Editor, and the StreamingAssets folder in a build
    string filePath = Path.Combine(Application.streamingAssetsPath, "Data.json");
    if (File.Exists(filePath))
    {
        // Read the json from the file into a string
        string dataAsJson = File.ReadAllText(filePath);
        // Pass the json to JsonUtility, and tell it to create a GameData object from it
        GameData loadedData = JsonUtility.FromJson<GameData>(dataAsJson);

        // Retrieve the allRoundData property of loadedData
        Questions = loadedData.Questions ;
    }
    else
    {
        Debug.LogError("Cannot load game data!");
    }
}

I try these codes, but nothing happens: First code:

void Start()
{
    string filePath = "jar:file://" + Application.dataPath + "!/assets/SahAwKhataa.json";
    StartCoroutine(GetDataInAndroid(filePath));
}

IEnumerator GetDataInAndroid(string url)
{
    WWW www = new WWW(url);
    yield return www;
    if (www.text != null)
    {

        string dataAsJson = www.text;
        GameData loadedData = JsonUtility.FromJson<GameData>(dataAsJson);

        Questions = loadedData.Questions ;
    }
    else
    {
        Debug.LogError("Can not load game data!");
    }
}

The second:

public void ReadJson()
{
    string path = Application.streamingAssetsPath + "/Data.json";
    #if UNITY_EDITOR
    //string path = "/StreamingAssets/Data.json";
    #endif
    WWW www = new WWW(path);
    while (!www.isDone) { }
    string json = www.text;
    GameData loadedData = JsonUtility.FromJson<GameData>(json);
    Questions = loadedData.Questions;
}

GameData script code:

[System.Serializable]
public class GameData 
{
    public GameQuestions[] Questions;
}

GameManager script code:

public class GameManager : MonoBehaviour {

private GameQuestions[] Questions;
private static List<GameQuestions> LevelQuestions ; 

private string gameDataFileName = "Data.json";

private GameQuestions CurrentQuest;

void Start()
{
    TestText.text = "Text1";
    StartCoroutine(LoadStreamingAsset(gameDataFileName));
    TestText.text = "Text2";

    if (LevelQuestions == null || LevelQuestions.Count == 0)
    {
        TestText.text = "Text3";
        LevelQuestions = Questions.ToList<GameQuestions>();
    }
    TestText.text = "Text4";
    int QuestNumber = Random.Range(0, LevelQuestions.Count);
    CurrentQuest= LevelQuestions [QuestNumber ];

    QuestText.text =  CurrentQuest.Quest;
}
 IEnumerator LoadStreamingAsset(string fileName)
{
    string result;
    string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);

    if (filePath.Contains("://") || filePath.Contains(":///"))
    {
        WWW www = new WWW(filePath);
        yield return www;
        result = www.text;
    }
    else
    {
        result = File.ReadAllText(filePath);
    }
    GameData loadedData = JsonUtility.FromJson<GameData>(result);
    Questions= loadedData.Questions;
    TestText_0.text = loadedData.Questions[0].quest;
}

GameQuestions script code:

[System.Serializable]
public class GameQuestions 
{

public string quest;
public bool isCorrect;
}

Data.json:

{"Questions":[{"quest":"Question 1","isCorrect":true},{"quest":"Question 2","isCorrect":false},{"quest":"Question 3","isCorrect":true}]}
Meqara
  • 23
  • 5
  • I suspect that you put the json in the wrong place. Where did you put the json file in your project? – Programmer Jun 05 '18 at 10:09
  • In the "StreamingAssets" folder. – Meqara Jun 05 '18 at 10:11
  • There is a `loadStreamingAsset` function from the duplicate. You have to try that. It uses `WWW` on Android an `File.ReadAllText` other devices. `File.ReadAllText` doesn't work on Android for the StreamingAssets folder. Try that – Programmer Jun 05 '18 at 10:16
  • I try WWW but not works. I edit my question (I add some code I tried). – Meqara Jun 05 '18 at 10:36
  • Again, check the `loadStreamingAsset` function from my answer in the duplicate. It has what you are looking for. What you have now in your edit is not what's in that answer. If you can't tell the difference just copy it directly – Programmer Jun 05 '18 at 11:00
  • I check it and I add after the result variable this two lines of code: "GameData loadedData = JsonUtility.FromJson(result); Questions= loadedData.Questions;" is this correct because it didn't works? – Meqara Jun 05 '18 at 11:15
  • I mean after "result = www.text" line. – Meqara Jun 05 '18 at 11:21
  • Update your question with your new code from the duplicate. Don't worry about ` JsonUtility.FromJson` at this time. Just worry about reading the json. If you can't read it it then `JsonUtility.FromJson` won't work. You can use a simple Text component to try and display the result of `www.text`. Test it on the Editor before testing it on Android to see if you can load it. Finally, edit your question with that code. – Programmer Jun 05 '18 at 11:27
  • I add a text object, and it shows me the content of my file (Data.json), but 'JsonUtility.FromJson' doesn't work on android, only works on the editor. – Meqara Jun 05 '18 at 12:28
  • Ok, good. Solved the reading part. Do **3** things to your question:1 Edit your question with that new code. That code is where I will continue to help with. 2.Also post your the jon objects like GameData and Questions classes. 3.Post what the json looks like. I will tell you what to do next – Programmer Jun 05 '18 at 18:07
  • I edit the question with the script's code. And I am very sorry for what happened with the other account I didn't notice that you answered me. – Meqara Jun 05 '18 at 19:17
  • Np. Add the text component again but this time try put `textObject.text = loadedData.Questions[0].quest` after `GameData loadedData = JsonUtility.FromJson(result);` and see if you can see the first quest data. – Programmer Jun 05 '18 at 19:27
  • It displays first quest data. – Meqara Jun 05 '18 at 19:57
  • On Android too? – Programmer Jun 05 '18 at 20:05
  • Yes, on Android too. And I edit the GameManager script in the question above, I add the code that displays the questions in my game (not the test textObject that we add earlier) – Meqara Jun 05 '18 at 20:23
  • After testing my code for several hours by adding textObject after and before some line of codes, I found that the problem is in this code "LevelQuestions = Questions.ToList();", the textobjects after this line don't display their texts, but if I comment it, the textobjects after it display their texts. But I didn't found the solution, unfortunately. – Meqara Jun 05 '18 at 23:29
  • Wait, is the json supposed to finish loading before the code only `StartCoroutine(LoadStreamingAsset(gameDataFileName));` such as `LevelQuestions = Questions.ToList();` should run? – Programmer Jun 05 '18 at 23:42
  • I add the TestText to the code above. If the code line that I mention is not commented the "Text4" will not appear (only "Text3" will appear), but if I comment it, the "Text4" will be displayed. And I think the StartCoroutine finish loading because "Text3" and "Text4" appear, also the TestText_0 is displayed normally in the function"LoadStreamingAsset()", is that what you mean? – Meqara Jun 06 '18 at 01:01
  • It's not what I am asking. I am asking you if the `LoadStreamingAsset(gameDataFileName)` function is supposed to finish before the `if (LevelQuestions == null || LevelQuestions.Count == 0)` code below it runs. If you don't know the answer for that then I can't help you since it looks like you didn't write that code. Note that the problem now is not loading the json file or de-serializing the json to object. The problem now is something else – Programmer Jun 06 '18 at 01:09
  • I think I get what you mean, and you are right, the `LevelQuestions = Questions.ToList();` processed before the `LoadStreamingAsset(gameDataFileName)` finished, so I add a boolean so I can check if it is finished before it continues and it works, thank you very much. – Meqara Jun 06 '18 at 12:16
  • I have an other question about encoding in android (Arabic letter) can I ask here, or should I open a new question? – Meqara Jun 06 '18 at 12:18

0 Answers0