In Unity 5.4, I have a JSON file I can successfully parse via JsonUtility.FromJson when I fetch it locally from StreamingAssets, but fetching that same same file via WWW throws an error (ArgumentException: JSON parse error: Invalid value.) when I fetch it from a remote server.
I can output both the local and remote (jsonString) files just prior to parsing by JsonUtility, and they are identical (I've even validated each in a JSON validator.)
Here's the code I'm using to retrieve and parse the remote JSON:
void Prime(){
string url = "https:content_url.json";
WWW www = new WWW(url);
StartCoroutine(WaitForContentJSON(www));
}
IEnumerator WaitForContentJSON(WWW contentData)
{
yield return contentData;
// check for errors
if (contentData.error == null)
{
ParseJSON(contentData.text);
} else {
Debug.Log("WWW Error: "+ contentData.error);
}
}
void ParseJSON(string jsonString){
var ac = JsonUtility.FromJson<ArticlesCollection>(jsonString);
}
The error is thrown inside ParseJSON when invoking JsonUtility.FromJson
Any help much appreciated.
EDIT: Adding JSON per @Programmer's request
JSON returned from local file via File.ReadAllText:
{
"articles": [ {
"articleID": "1",
"title": "Life & Death at the Mexican Border",
"byline": "Part 1 of Life and Death...",
"longDescription": "Part 1 of Life and Death...",
"imageURL": "http://foo.jpg",
"videoURL": "http://foot.mp4",
"sceneAssetBundle": "scene_bundle_1",
"sceneName": "scene_1",
"featured": true,
"duration": "7:12",
"videoSize": "625"
}, {
"articleID": "2",
"title": "Lake Mead",
"byline": "The shrinking water....",
"longDescription": "Welcome...",
"imageURL": "http://vfoo.jpg",
"videoURL": "http://food.mp4",
"sceneAssetBundle": "scene_bundle_2",
"sceneName": "scene_2",
"featured": true,
"duration": "1:45",
"videoSize": "151"
}, {
"articleID": "3",
"title": "Visi...",
"byline": "Experience...",
"longDescription": "Experience...",
"imageURL": "http://foo.jpg",
"videoURL": "http://foo.mp4",
"sceneAssetBundle": "scene_bundle_2",
"sceneName": "scene_2",
"featured": false,
"duration": "5:46",
"videoSize": "478"
} ]
}
JSON returned from remote (S3):
{
"articles": [ {
"articleID": "1",
"title": "Life & Death at...",
"byline": "Part 1 of...",
"imageURL": "http:foo.jpg",
"videoURL": "http://foo.mp4",
"featured": true,
"duration": "7:12",
"videoSize": "625"
}, {
"articleID": "2",
"title": "Lake Mead",
"byline": "The...",
"longDescription": "Welcome...",
"imageURL": "http://foo.jpg",
"videoURL": "http://foo.mp4",
"featured": true,
"duration": "1:45",
"videoSize": "151"
}, {
"articleID": "3",
"title": "Visit",
"byline": "Experience...",
"longDescription": "Experience the...",
"imageURL": "http:foo.jpg",
"videoURL": "http://foo.mp4",
"featured": false,
"duration": "5:46",
"videoSize": "478"
} ]
}
Again, I've validated both of these JSON files in a validator, and again the JsonUtility.FromJson call works fine when passed the JSON fetched locally but errors when passed the JSON from the remote source fetched via WWW
And, per @dbc's request I'm posting the body of my ArticlesCollection and Articles wrapper classes into/against(?) which the JSON is parsed. But again, this works fine when fetching the JSON locally, so I don't suspect there's an issue in these files.
ArticlesCollection:
using UnityEngine;
[System.Serializable]
public class ArticlesCollection
{
public Article[] articles;
}
Articles:
using UnityEngine;
[System.Serializable]
public class Article
{
public string title;
public int articleID;
public string byline;
public string longDescription;
public string imageURL;
public string experienceURL;
public bool featured;
public string duration;
public string experienceSize;
public string sceneAssetBundle;
public string sceneName;
}