Android devices does not read JSON file, but Unity able to read it. Why?

Hi, I’m using Json/LitJson file to read the database question and answer into my quizzes.

It works without any error on Unity, but after I build it into APK, it did not run properly on the android devices, which the quizzes did not came out after I start it. I’m using MI3 android phone, then I tried it on BlueStacks also. It does not work on both platform(well both of them is android).

I’m not very sure it is the android devices problem or my read Json file code problem. Below is my code to read the Json file. Appreciate any helps. Thanks

void Start(){//start reading Json file
	score = 0;
	nextQuestion = true;
	jsonString = File.ReadAllText (Application.dataPath + "/quiz.json");
	questionData = JsonMapper.ToObject (jsonString);
	//StartCoroutine ("Json");
}
public void OnClick(){//get the question and answer from Json after clicking

	if (nextQuestion) {

		GameObject[] destroyAnswer = GameObject.FindGameObjectsWithTag ("Answer");
		if (destroyAnswer != null) {
			for (int x=0; x<destroyAnswer.Length; x++) {
				DestroyImmediate (destroyAnswer [x]);
			}
		}

		qq.GetComponentInChildren<Text> ().text = questionData ["data"] [questionNumber] ["question"].ToString ();

		for (int i=0; i<questionData["data"][questionNumber]["answer"].Count; i++) {
			GameObject answer = Instantiate (answerPrefab);
			answer.GetComponentInChildren<Text> ().text = questionData ["data"] [questionNumber] ["answer"] *.ToString ();*
  •   		Transform answerC = GameObject.Find ("GameObject").GetComponent<Transform> ();*
    
  •   		answer.transform.SetParent (answerC);*
    
  •   		string x = i.ToString ();*
    
  •   		if (i == 0) {*
    
  •   			answer.name = "CorrectAnswer";*
    
  •   			answer.GetComponent<Button> ().onClick.AddListener (() => Answer ("0"));*
    
  •   		} else {*
    
  •   			answer.name = "WrongAnswer" + x;*
    
  •   			answer.GetComponent<Button> ().onClick.AddListener (() => Answer (x));*
    
  •   		}*
    
  •   		answer.transform.SetSiblingIndex (Random.Range (0, 3));*
    
  •   	}*
    
  •   	questionNumber++;*
    
  •   }*
    
  • }*

To read files, stock them in a folder Assets/Resources/ then load them with Resources.Load and turn your file content into a TextAsset.

(to be more precise, Assets is your Assets folder, and Resources is a “special folder” Unity can reconize for loading resources when you create it)

Here is an example :

// Resources.Load look files from the root of the Resources folder
TextAsset file = Resources.Load("yourFileName") as TextAsset;
string content = file.ToString ();
// Then put your convert string -> JSON object after this

To save files if you need to, use persistentDataPath on mobile platforms :

This is more likely a large comment for debugging:

You are accessing json in Application.dataPath + "/quiz.json" right.

I’m think your json file saved inside <path to project folder>/Assets. folder

Inside android Application.dataPath means <path to player app bundle>/<AppName.app>/Data Read more

May be your json file is not there. if you can try this answer (get json through internet) may be we can check.

Its just a guess that because inside unity it works may be file not exist inside android

Not related to the answer but may help.
there are json files posted out there for developer to test. just grab a URL and test: example here

to nopesky

I also encounter the same problem that you had. There is a bigger problem because I used more than 1 json files. and I created the script to accommodate the files into two parts.
first part is Category
second is Questions and answers
I already used the script you posted here

TextAsset file = Resources.Load(“quiz”) as TextAsset;
Debug.Log (file);

My questions are:
a.(“quiz”) was your json file name or what?
b. you suggested to change the .json to .txt so does it mean I have to go to the file explorer and rename it?
I’m a newbie

What Blue-Cut did worked:

 // Resources.Load look files from the root of the Resources folder
 TextAsset file = Resources.Load("yourFileName") as TextAsset;
 string content = file.ToString ();
 // Then put your convert string -> JSON object after this

FURTHER I followed the same Inventory system tutorial as some who’ve commented here and elsewhere. Unity could not Resource.Load() my “Jsonfile.json” as a TextAsset as Unity doesn’t recognize .json as a textfile; changing it to .txt fixed that.

i am fetching text from url using simple json unity it is working fine in unity and iphone but not showing json text in android anyone faced similar issue please let me know if you have any solution.?