Hi there, I’ve been hitting my head against the wall for a few weeks now because of some weird behaviour I can’t quite figure out.
My webplayer has to read a JSON data file from a folder (inside the same server, in a subfolder, so the security sandbox doesn’t complain) getting the file via WWW class and using the JSONObject class to parse the data. I’ve coded it so in the case of the file not being available, it reads some default data from a JSON file from the resources folder. The thing is that when I use the WWW.text property to access the data from the downloaded file, the JSONObject class can’t read it, while whenever it reads the (very same) data from the file in the resources folder (using TextAsset.text property) there’s no problem at all. Both files are UTF-8 encoded.
I’ve narrowed the problem down to the point of WWW.text property vs TextAsset.text property reading files UTF-8 encoded and returning differently encoded strings (or something similar, because I’m lost). The docs on WWW.text say that the contents of the web page must be in UTF-8 or ASCII character set, which they are, but still, the string that I get using WWW.text can’t be read in the parser. The manual on TextAsset says that it can read .json files (which it does), so I really think that the problem may come from reading a UTF-8 encoded file with the WWW class.
The (updated) code:
WWW myWWW = new WWW(Application.dataPath + JSONurl);
yield return myWWW;
string jsonData = "";
if (myWWW.error == null) {
jsonData = myWWW.text;
} else {
TextAsset myData = (TextAsset)Resources.Load(JSONFile, typeof(TextAsset));
jsonData = myData.text;
}
JSONObject json = new JSONObject(jsonData);
Any ideas on why WWW.text could be returning a wrong string while reading from a UTF-8 encoded file?
Thanks in advance.
[UPDATE] I’ve updated the code and done some more tests. The string that receives the data from the WWW.text displays the contents of the file correctly, no matter the encoding of the file (ANSI or UTF-8). Looks like the string returned by WWW.Text and TextAsset.text might be in different encodings and the JSONOBject only accepts the encoding from the TextAsset.text string. I’ll keep working on it.