在Unity 5.4中,当我从StreamingAssets本地获取时,我可以通过JsonUtility.FromJson成功解析一个JSON文件,但是通过WWW获取同一文件时会抛出错误(ArgumentException:JSON parse error:Invalid value。)。从远程服务器。
我可以在JsonUtility解析之前就输出本地和远程(jsonString)文件,它们是相同的(我什至已经在JSON验证程序中对每个文件进行了验证。)
这是我用来检索和解析远程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);
}
调用JsonUtility.FromJson时,在ParseJSON内部引发错误。
任何帮助,不胜感激。
编辑:根据@Programmer的请求添加JSON
通过File.ReadAllText从本地文件返回的JSON:
{
"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"
} ]
}
从远程(S3)返回的JSON:
{
"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"
} ]
}
再次,我已经在验证器中验证了这两个JSON文件,并且再次传递了JsonUtility.FromJson调用,该调用在传递本地获取的JSON时工作正常,但是从通过WWW获取的远程源传递JSON时出错
并且,按照@dbc的请求,我将我的ArticlesCollection和Articles包装器类的正文发布到JSON解析的/ against(?)中。但是同样,当在本地获取JSON时,此方法工作正常,因此我不怀疑这些文件中存在问题。
文章收藏:
using UnityEngine;
[System.Serializable]
public class ArticlesCollection
{
public Article[] 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;
}
参考方案
由于使用的是Unity 5.4,因此不应将WWW
用于Web请求。 UnityWebRequest
替换了WWW
,它解决了3个额外字节的问题。使用它的原因还有很多,例如对https的支持。
IEnumerator WaitForRequest(string url)
{
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.Send();
if (www.isError)
{
Debug.Log("Error: " + www.error);
}
else
{
Debug.Log("Downloaded: " + www.downloadHandler.text);
// byte[] results = www.downloadHandler.data;
ArticlesCollection article = JsonUtility.FromJson<ArticlesCollection>(www.downloadHandler.text);
}
}
如何从php中获取datatables jQuery插件的json数据 - php我是PHP的新手,正在尝试使用Datatables jQuery插件。我知道我必须从.php文件中获取数据,然后在数据表中使用它,但是我似乎无法通过ajax将其传递给数据表。我设法从数据库中获取数据,并对其进行json编码,但是后来我不知道如何在我的index.php文件中调用它,并在其中显示它。我究竟做错了什么?这是我的代码:HTML(已编辑): <…
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
改造正在返回一个空的响应主体 - java我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…
每个文件合并后添加换行 - python我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…
Json到php,json_decode返回NULL - php我正在用PHP进行JSON解析器的一些API,用于存储有关遗产的信息。我在解析时遇到问题,因为它返回的是NULL值而不是数组或对象。简单的JSON代码可以很好地解析,但是可以这样:{"success":true,"totalCount":1,"data":[{"id":99694…