Loading JSON file from streamingAssets on android
Hi there.
I'm trying to load a list of country codes i have stored in the streamingAssets
folder. I'm able to load it properly with iOS, but on android, using a WWW request, i am getting a JSON parse error. When I log the contents of the file to the console, i.e. www.text
, I see the data gets cut off halfway.
Is this a limitation of the WWW
class?
The file is valid json and loads just fine on iOS.
This is the code i'm using to load the file:
public IEnumerator LoadCountryCodes() {
string filePath = Path.Combine(Application.streamingAssetsPath, "CountryCodes.json");
string dataAsJson;
if (filePath.Contains ("://")) {
WWW www = new WWW (filePath);
yield return www;
dataAsJson = www.text;
} else {
dataAsJson = File.ReadAllText(filePath);
}
CountryCodes countryCodes = JsonUtility.FromJson<CountryCodes>(dataAsJson);
if (OnCodesLoadedMethods != null) {
OnCodesLoadedMethods (countryCodes.codes);
}
}
Have you tried using the 'bytes' property ins$$anonymous$$d of 'text' ?
byte[] dataAsBytes = www.bytes;
string dataAsJson = System.Text.Encoding.Default.GetString (bytes);
Answer by radiodario · May 23, 2017 at 09:10 AM
Hey I loaded the file from Resources
as a TextAsset
and then converted it to JSON, and that worked
public void LoadCountryCodes() {
TextAsset codesJson = Resources.Load<TextAsset> ("CountryCodes");
CountryCodes countryCodes = JsonUtility.FromJson<CountryCodes>(codesJson.text);
Debug.Log ("Country codes loaded: " + countryCodes.codes.Length);
if (OnCodesLoadedMethods != null) {
OnCodesLoadedMethods (countryCodes.codes);
}
}
Thanks for your help @blue-mile
Interesting. But it doesn't solve your problem in case you really want your JSON file to be a strea$$anonymous$$g asset ins$$anonymous$$d of a resource (a strea$$anonymous$$g asset could be split from binary on Android for example).
I wonder if this is an issue with the WWW.text property or with JsonUtility. Have you tried writing down WWW.text back into a file ? BTW, what is the size of your file ?
Your answer
Follow this Question
Related Questions
Loading JSON from StreamingAssets on android(c#) 0 Answers
Read and Write Textfile on mobile (C# Unity ) 0 Answers
Help Writing and reading files on android. 1 Answer
Ressource.Load returns null on "build" but work on "build and run" 2 Answers
Loading from streaming assets in obb fails.. but not always. 1 Answer