Unity:从远程源解析JSON时出错 [英] Unity: Error Parsing JSON from Remote Source
问题描述
在Unity 5.4中,当我从StreamingAssets本地获取JSON文件时,可以通过 JsonUtility.FromJson 成功解析该JSON文件,但是可以通过 WWW 提取相同的文件当我从远程服务器上获取错误时,会引发错误( ArgumentException:JSON解析错误:无效值.).
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.
在通过 JsonUtility 进行解析之前,我可以输出本地和远程( jsonString )文件,它们是相同的(我什至已经在JSON中进行了验证)验证器.)
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.)
这是我用来检索和解析远程JSON的代码:
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);
}
调用 JsonUtility.FromJson
非常感谢您的帮助.
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:
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"
} ]
}
同样,我已经在验证器中验证了这两个JSON文件,并且 JsonUtility.FromJson 调用在通过本地获取的JSON时工作正常,但是从远程源传递JSON时出错通过 WWW
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
然后,按照@dbc的请求,我将 ArticlesCollection 和 Articles 包装器类的正文发布到JSON解析的/against(?)中.但是同样,当在本地获取JSON时,此方法工作正常,因此我不怀疑这些文件中存在问题.
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;
}
文章:
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的支持.
Since you are using Unity 5.4, you shouldn't be using WWW
for web requests. UnityWebRequest
replaced WWW
and it solves that problem of 3 extra bytes. There are just many other reasons to use it such as support for 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);
}
}
这篇关于Unity:从远程源解析JSON时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!