Deserialize JSON data for android via WWW (JsonUtility / JsonFX)
So I have to deserialize data from a file. It works fine for Windows and Mac, but doesnt for Android or iOS.
This is the code I have for Windows:
public void LoadAndDeserialize()
{
var streamReader = new StreamReader(PATH);
data = streamReader.ReadToEnd();
streamReader.Close();
questions = JsonUtility.FromJson<Questions> (data);
}
It works perfectly. Either with JSONFX or new JsonUtilities. And this is the code I have for Android:
public void LoadAndDeserialize()
{
WWW adata = new WWW(PATH);
while ( !adata.isDone) {}
var streamReader = new StreamReader(adata.text);
data = streamReader.ReadToEnd();
streamReader.Close();
questions = JsonUtility.FromJson<Questions> (data);
}
I also tried deserializing directly from WWW:
questions = JsonUtility.FromJson<Questions> (adata.text);
But it wont work either. So, what Im doing wrong? Whats the way to deserialize data for Android?
What does your 'Questions' type definition look like?
Answer by Heissayen · Jan 12, 2016 at 03:11 PM
I managed to work around the problem by storing the data on Resources folder and loading the asset as TextAsset.
Someone suggested I needed to use Trim() in order to get it to work with WWW.
Here's the post and the answers in detail: https://www.reddit.com/r/Unity3D/comments/3w7mhx/deserialize_json_data_for_android_via_www/
Answer by Heissayen · Dec 10, 2015 at 02:03 PM
What I mean with "wont work" is: when I try to assign each field of Questions (Question text, answers..) to a GUI text I get a NullReferenceException: Object reference not set to an instance of an object. This does not happen for windows, and I'm assuming something is not working properly with the deserialization from WWW.
Your answer
![](https://koobas.hobune.stream/wayback/20220612052057im_/https://answers.unity.com/themes/thub/images/avi.jpg)