Unity:从远程源解析JSON时出错

时间:2016-08-06 04:24:14

标签: c# json unity3d

在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

JSON通过 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"
  } ]
}

从远程(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的请求,我将 ArticlesCollection Articles 包装类的主体发布到/反对(?),其中解析了JSON。但同样,这在本地获取JSON时工作正常,所以我不怀疑这些文件存在问题。

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;
}

2 个答案:

答案 0 :(得分:4)

由于您使用的是 Unity 5.4 ,因此您不应该使用WWW来处理网络请求。 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);
    }
}

答案 1 :(得分:1)

所以答案如本文http://answers.unity3d.com/questions/844423/wwwtext-not-reading-utf-8-text.html

所述

这是针对我的问题的经过验证的修复程序。

问题似乎是响应的头部正好有3个额外的字节。修复是使用 WWW.bytes 而不是 WWW.text ,然后切掉额外的3个字节。正是如此,

string jsonString;
jsonString = System.Text.Encoding.UTF8.GetString(contentData.bytes, 3, contentData.bytes.Length - 3);

考虑到我使用 WWW.text 指出的所有文档,这一点特别奇怪。似乎Unity应该向 WWW 添加一个数据属性来解决这个问题。