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 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.
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: Reddit - Dive into anything