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 ();*
Hi Sir, I have the same problem just like you and a code just like yours about question quiz, I may ask some question how did you do first in order to display the json data into mobile, because in my case the json is not appear. I have a set of .json files in my Streaming Assets folder and a LitJson.dll. I dont know what is wrong in my code but it has no error in the console. I dont know what to do and how to fix this.. please help me as possible. please. :(
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 :
sorry, but I'm kinda newbie on Unity and C#. So you mean I just need to change my way of reading Json fle on the void Start() function? How about the others parts that read out the data inside the Json file, remain the same?
Thanks. The thing I do not understand after I use Resources.Load to read my Json file, the way to call out the data inside my Json file will remain the same, or not. for example: qq.GetComponentInChildren ().text = questionData ["data"] [questionNumber] ["question"].ToString (); questionNumber++; The code above is the way to call out a data from Json file, after using Resources.Load, does it remain the same way to call the data? Most importantly, I still dunno how to use Resources.Load, I tried googling it, not sure how to edit my code.
Hmm, I need to make the test myself to help you more, because the code I suggested to you is from a project that perfectly works on Android with csv files. I would like to know if the problem comes from a little mistake I am not seeing or from a special json managing with Unity that would make my method not efficient. Can you turn your .json into a .txt and tell me if file is still null ?
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>/DataRead 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
" I have zero understanding to those" Hmm. Didn't you write that code above?. i suggest you to follow some tutorials on this topic (if you didn't). try @Blue Cat's answer, its promising. i stop ;). GL
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
// 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.
Yes, two different programs using two different audio devices is ok. But from what I understand he wants to make 1 program use 4 audio devices simultaneously, which is not possible.
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.?
Although I agree I don't think it's possible to do it in UNITY, I know it's possible for software to use multiple outputs. In SKYPE, I have it set to "ring" on my speakers, and i use my headset to talk/listen.
So, I could have my application launch another executable that communicates with my main application via local network and outputs sound to a different output device?
If it works inside Unity Maybe some kind of network issue. did you try to access json directly in your web browser.
– P3JXHi Sir, I have the same problem just like you and a code just like yours about question quiz, I may ask some question how did you do first in order to display the json data into mobile, because in my case the json is not appear. I have a set of .json files in my Streaming Assets folder and a LitJson.dll. I dont know what is wrong in my code but it has no error in the console. I dont know what to do and how to fix this.. please help me as possible. please. :(
– khelvino