c# - 团结 : Error Parsing JSON from Remote Source

标签 c# json unity-game-engine

在 Unity 5.4 中,我有一个 JSON 文件,当我从 StreamingAssets 本地获取该文件时,可以通过 JsonUtility.FromJson 成功解析它,但通过 WWW 获取相同的文件当我从远程服务器获取它时,抛出错误(ArgumentException:JSON解析错误:无效值。)。

我可以在 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 文件,并且在传递本地获取的 JSON 时,JsonUtility.FromJson 调用工作正常,但在从远程源传递 JSON 时出错通过WWW

获取

并且,根据 @dbc 的请求,我将我的 ArticlesCollectionArticles 包装类的主体发布到/反对(?),其中 JSON 被解析。但同样,在本地获取 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);
    }
}

关于c# - 团结 : Error Parsing JSON from Remote Source,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38800533/

相关文章:

c# - 就我而言,List.Add 中的数据为 Null

javascript - JSON 对象和 Javascript 对象

ios - 运行 Xcode 项目时如何修复 IUnityGraphicsMetal 错误

c# - 在 parallel.ForEach 循环中获取线程 ID

c# - 来自 C# : why do I have to override new/delete? 的 mingw DLL

javascript - 将 HTML 负载解析为 JSON

c# - Unity协程无法跨场景工作

unity-game-engine - if 语句表现不佳

c# - 如何处理从存储过程到 C# 代码的空值参数返回?

java - 将 HTTP "_method" header 传递给 matlab 中的 urlread2 函数