- Home /
How to read json file in android game?
Hi there,
I have few set of data store in different json file in a folder.When my game run, it will select one of the json file and load the data into the game. I use streamReader and specific the file path for the game to read the file.
When i test the game in pc, it works fine. But when i run the game in my mobile phone which is android platform, the game cant read the data from the json file. After i search on internet, it seems the file location in pc and android are different so i have to make change on the file path setting.
I had try on Application.datapath, Application.persistentDataPath, www method and change the write access in build setting to external(sd card), but my game still cant read the data inside the json file on my phone.
May i know what is the correct way to do read file in android or do i miss out some step?
Thank you.
Below is my example code on how i read the json file and load data into game.
void Start()
{
Instance = this;
StreamReader readJson = new StreamReader(Application.persistentDataPath + FoodMenu.foodname + ".json");
response = readJson.ReadLine();
JsonData Json = JsonMapper.ToObject(response);
//Set Recipe Name
recipeName = (string)Json["RecipeName"];
//Set time
time = (int)Json["Time"];
How do you get the json files onto the device? And where do you put them?
I already have few sample json files inside the game folder to do game testing.When i build the game into the apk, the json files are inside the apk file also.
i keep all the json file in a folder call JSON and put the folder inside the script folder.(i.e:Assets/script/JSON)
Answer by Kamuiyshirou · Jan 06, 2015 at 01:54 PM
Try this: You need to change the build settings for android. change configuration > write access to external (sd card).
I already do the setting change like you say but still cant read the data inside the json file.(sorry if i not stated my setting change was for android in the post.)
Your answer
Follow this Question
Related Questions
Android - Straight answer for accessing data via Application.dataPath 1 Answer
Json not recognized on Android 1 Answer
Can't Access AppData Folder from Unity 1 Answer
Cannot access JSON file from Streaming Assets in android build 1 Answer
Difference between Application.persistantDataPath Vs Application.dataPath 2 Answers